Tab Atkins Jr. jackalmage at
Fri Apr 28 21:27:20 UTC 2017

On Mon, Apr 24, 2017 at 10:53 PM, Raul-Sebastian Mihăilă
<raul.mihaila at> wrote:
> On Tue, Apr 25, 2017 at 12:15 AM, Tab Atkins Jr. <jackalmage at>
> 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.

Ah, these use-cases are reasonable.

In that case, then, yeah, checking `` seems to be the way
you want to go. That's explicitly what it was defined to do:

    if( throw "Foo() must not be called with new";

There's no shorter way to do this; all the shorter methods rely on the
function clearly existing as a method, syntactically.


More information about the es-discuss mailing list