javascript - Why calling array.prototype.forEach.call() with an array set to THIS object not working -
here have number array. tried test if array.prototype.foreach can used on array in different way traditional way. in traditional way pass argument second parameter foreach.
here used array.prototype.foreach.call() , used array argument call method.
indication window object.
why ?
number=[1,2,3,4,5]; array.prototype.foreach.call(number,function(elem){ console.log(this); });
because assuming foreach has not been overwritten, , array still has normal prototype, this:
array.prototype.foreach.call(number,function(elem){ }); is no different from:
number.foreach(function(elem){ }); in foreach callback function, unless pass thisarg, function called normal callback.
if
thisargparameter providedforeach(), passedcallbackwhen invoked, usethisvalue. otherwise, valueundefinedpassed usethisvalue.thisvalue observablecallbackdetermined according the usual rules determining seen function.
to set thisarg while using .call, need pass 1 more argument:
array.prototype.foreach.call(number,function(elem){ }, number);
Comments
Post a Comment