The following code does not seem to run as expected under Chrome, and runs differently in Firefox.
(function () {
'use strict';
var
arr = Object.freeze([1, 2, 3]);
try {
arr.push(4);
} catch (e) {
console.log(e);
}
try {
console.log(arr.pop());
}catch (e) {
console.log(e);
}
console.log(arr);
})();
I expected that the output would be:
Error : (for `arr.push(4)`)
Error : (for `arr.pop()`)
[1, 2, 3]
but when running this code on Chrome 29.0.1547.49 (Official Build 216092) beta-m, I receive the following output:
3
[1, 2, 3]
Why is there no exception? I ran this code on Firefox Nightly 26.0a1(2013-08-12), and the result is
TypeError: arr.push(...) is not extensible
TypeError: property arr.pop(...) is non-configurable and can't be deleted
[1, 2, 3]
as I had expected.
I thought about why the difference between Chrome and Firefox, then I realized that it could be because of strict mode of pop and push methods. To sum up, in Firefox (SpiderMonkey) pop and push methods are defined in strict mode, but in Chrome (V8) these methods isn't defined in strict mode.
I don't know what is the actual specification. (I read some ECMA-262 5.1th Edition, but I can't find such section.)
ECMA 262 5.1 says the following of
Array.prototype.push:Notice how the argument 3 to
[[Put]]istrue. Now,[[Put]]is defined as[[CanPut]]then returnsfalseamong others, in the case of array if[[Extensible]]onOisfalse.Thus, your Chrome is in violation of the ECMA 262 5.1 specification.
Update:
Chrome developers are talking about making the
push,poprun under strict mode; however the difference is not just "strict" vs "non-strict" as the behaviour ofpushandpopis specified very specifically in the ECMA 262 5.1 specification.