Nonconstructors
Raul-Sebastian Mihăilă
raul.mihaila at gmail.com
Tue Apr 25 05:53:55 UTC 2017
On Tue, Apr 25, 2017 at 12:15 AM, Tab Atkins Jr. <jackalmage at gmail.com>
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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170425/37f366ff/attachment.html>
More information about the es-discuss
mailing list