Code smell? Iterator prototype has iterator method that returns "this"

Claude Pache claude.pache at gmail.com
Wed Jul 27 07:16:26 UTC 2016


> Le 27 juil. 2016 à 08:56, Claude Pache <claude.pache at gmail.com> a écrit :
> 
> 
>> Le 27 juil. 2016 à 07:16, Jordan Harband <ljharb at gmail.com> a écrit :
>> 
>> `new weirdFunction()` would `=== iter` because a constructor that returns an object (`this`), returns that object when `new`ed - which is how functions have worked since ES3 (or probably ES1).
> 
> If `weirdFunction` were defined as if by evaluating `function () { return this }`, yes. But in general, functions are not necessarily constructors (just try `new Math.sin` in your favourite JS environment).
> 
> —Claude
> 

Correction: If `weirdFunction` were defined as if by evaluating `function () { return this }`, **no**: in such a constructor (more precisely: when such a function is used as a constructor), `this` is a brand-new "instance"; it can’t be === to a previously existing object such as `iter`.

(Given how confusing and useless it is, I hope that my intuition ("not a constructor") is correct.)

—Claude



More information about the es-discuss mailing list