4 ms·
> They're both (subjectively) wrong: the natural place for data in JS is the this parameter. ... said the person not understanding the language semantics. Pas
by the_mitsuhiko 11y ago
> They're both (subjectively) wrong: the natural place for data in JS is the this parameter.
... said the person not understanding the language semantics.
Passing arbitrary data as this is a terrible idea. It has to be an object and as such all primitives end up boxed.
- inglor 11y agoNot in strict mode... `(function(){ console.log(typeof this); })(5);` try in sloppy and strict mode - remember modules are strict mode by default.
- codecurve 11y ago(function double() { return this * 2; }).call(2) === 4 I didn't have to box anything to pass a primitive as this. Maybe some other versions are different, but this works in Chrome 40 and Firefox 33.
- implicit 11y agoAre you sure? > function isboxed() { return this instanceof Number; } > isboxed.call(5); true
- CrossEye 11y agoas inglor said, this depends on strict mode: > var isboxed = (function() {"use strict"; return function isboxed() {return this instanceof Number;}}()); > isboxed.call(5); false