On Tue, Apr 25, 2017 at 12:15 AM, Tab Atkins Jr. <[email protected]>
 wrote:

>
> The obvious question is, why do you want to use `this`?
>
> ~TJ
>


For cases such as a debounce function:

```js
  const debounce = (func, delay) => {
    let timeout;

    return function (...args) {
      clearTimeout(timeout);

      timeout = setTimeout(() => {
        func.apply(this, args);
      }, delay);
    };
  };
```

If func is using `this` the resulted function (returned by `debounce`)
should pass its `this`.
Another case is related to form fields. Let's say a form field is an object
with a `validate` method. I provide a `createValidator` function to the
user that accepts multiple functions in order to call them 1 by 1 until one
of them returns an error. The result of `createValidator` is a function
that then becomes the value of the field's `validate` method (which the
user will call in order to validate the field). So the function will want
to use `this` because it will forward it to the functions provided by the
user. This is expected because the function will be called as a method of
the field object.
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to