Closed Bug 948407 Opened 12 years ago Closed 12 years ago

`Function.prototype.apply` & `Function.prototype.call` with `undefined` or `null` as `thisArg`

Categories

(Core :: JavaScript Engine, defect)

x86
macOS
defect
Not set
normal

Tracking

()

RESOLVED INVALID

People

(Reporter: mathias, Unassigned)

References

Details

User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36 Steps to reproduce: From <http://ecma-international.org/ecma-262/5.1/#sec-15.3.4.3> and <http://ecma-international.org/ecma-262/5.1/#sec-15.3.4.4>: The `thisArg` value is passed without modification as the `this` value. This is a change from Edition 3, where a `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. It seems like modern engines still have the ES3 behavior: function foo() { console.log(this); return this; }; foo.call(undefined) === undefined; // `false`, expected `true` I’ve tested this in Spidermonkey/Firefox, Carakan/PrestOpera, JSC/Safari, and v8/Chrome. They all show FAIL in this test case: data:text/html,<script>function foo() { console.log(this); return this; }; document.write(foo.call(undefined) === undefined %3F 'PASS' %3A 'FAIL');</script> Is this… 1. a wilful violation of the ES5 spec for back-compat reasons, or… 2. is it just an oversight that this never got implemented, or… 3. am I misreading the spec? If 2 is the case, please fix this :)
This is a bug in the spec. See paragraph 11 of annex C[1] for language clearly stating that this is strict mode-only. So it's actually none of your 1-3 ;) (Well that, or I'm misreading the spec, too.) As the bug still exists in the ES6 draft, please file a bug against the spec, if there isn't one already. [1]: http://ecma-international.org/ecma-262/5.1/#sec-C
Status: UNCONFIRMED → RESOLVED
Closed: 12 years ago
Resolution: --- → INVALID
Thanks! Spec bug filed.
The spec seems to be just fine to me. call() and apply() end up calling the function's [[Call]], which lands you at http://ecma-international.org/ecma-262/5.1/#sec-13.2.1 and then step 1 sends you over to http://ecma-international.org/ecma-262/5.1/#sec-10.4.3 and then steps 1-3 massage "this" based on strict mode and whatnot.
Did you perhaps mistake the [[Call]] of a function for the script that runs when the functions is called? They're not the same thing...
So afaict, the issue is in fact comment 0 item 3.
Gotta love layer 8 issues. Thanks for clarifying.
You need to log in before you can comment on or make changes to this bug.