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.

from mdn:

if thisarg parameter provided foreach(), passed callback when invoked, use this value. otherwise, value undefined passed use this value. this value observable callback determined 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

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -