javascript generator value of this -
what value of this
in javascript generators in javascript? in below code, both comparisons return false, , when .tosource()
, this
appears empty object
. references ecma or mdn docs helpful, wasn't able find in either.
function thisgenerator(){ while(1) yield this; } var gen=new thisgenerator(); alert(gen.next()==thisgenerator); alert(gen.next()==gen);
this
still obeys normal rules. considering, global scope window
:
var gen = (function() { yield this; })(); gen.next() === window // true var gen = (function() { "use strict"; yield this; })(); gen.next() === undefined // true
in quirks mode, this
in unbound functions global scope (which happens window
), while in strict mode undefined
.
ps: when calling function bound, still usual:
var o = { foo: function() { yield this; } }; o.foo().next() === o // true var o = {}; function foo() { yield this; }; foo.call(o).next() === o // true
Comments
Post a Comment