I have node v0.10.28 installed along with V8 v3.14.5.9 on Fedora 19. The issue I am having is with methods that have a thisArg
optional argument, like Array.prototype.forEach
.
If I execute the following code on Chromium v33 or Firefox v28 - jsFiddle
var y = [1, 2, 3];
y.forEach(function (element) {
console.log(this);
}, 'hej');
I get an output of
String {0: "h", 1: "e", 2: "j", length: 3}
String {0: "h", 1: "e", 2: "j", length: 3}
String {0: "h", 1: "e", 2: "j", length: 3}
And then the same code but in strict mode - jsFiddle
var y = [1, 2, 3];
y.forEach(function (element) {
'use strict';
console.log(this);
}, 'hej');
I get an output
hej
hej
hej
These are the results that I would expect as per the ECMA5 specification sec-function.prototype.call.
The thisArg value is passed without modification as the this value. This is a change from Edition 3, where an undefined or null thisArg is replaced with the global object and ToObject is applied to all other values and that result is passed as the this value. Even though the thisArg is passed without modification, non-strict mode functions still perform these transfromations upon entry to the function.
and for example sec-array.prototype.foreach
If a thisArg parameter is provided, it will be used as the this value for each invocation of callbackfn. If it is not provided, undefined is used instead.
and relevant pseudo code
Let funcResult be the result of calling the [[Call]] internal method of callbackfn with T as thisArgument and a List containing kValue, k, and O as argumentsList.
On node, however, both the above snippets return
{ '0': 'h', '1': 'e', '2': 'j' }
{ '0': 'h', '1': 'e', '2': 'j' }
{ '0': 'h', '1': 'e', '2': 'j' }
Can anyone confirm whether this is an issue with my node environment, or if this is a problem with node?
Update: just to confirm, in both cases on node typeof this
returns object
.