Situation:
I found something strange concerning strict mode in Javascript.
- I am using an external, third-party Javascript library which
- was minified,
- has over 4000 lines of code,
- is not using
use strict
at all, and - is using
arguments.callee
.
- I am using
use strict
in my own code, scoped within a function.
When I call one of the functions provided by the library, it throws an error. However,
- the error is thrown only if I am using
use strict
- the error is thrown in all browsers except Chrome
Code:
I've removed all the unrelated stuff and reduced the code into this (online demo on jsFiddle):
// This comes from the minified external JS library.
// It creates a global object foo.
(function () {
foo = {};
foo.bar = function (e) {
return function () {
var a5 = arguments.callee;
while (a5) {
a5 = a5.caller // Error on this line in all browsers except Chrome
}
}
}(any value here);
})();
// Here's my code.
(function() {
use strict; // I enable strict mode in my own function only.
foo.bar();
alert(done);
})();
Test result:
+-----------------------+-----+--------------------------------------------------------------+
| Browser | OS | Error |
+-----------------------+-----+--------------------------------------------------------------+
| Chrome 27.0.1453.94 m | Win | <<NO ERROR!>> |
| Opera 12.15 | Win | Unhandled Error: Illegal property access |
| Firefox 21.0 | Win | TypeError: access to strict mode caller function is censored |
| Safari 5.1.7 | Win | TypeError: Type error |
| IE 10 | Win | SCRIPT5043: Accessing the 'caller' property of a function or |
| | | arguments object is not allowed in strict mode |
| Chrome 27.0.1543.93 | Mac | <<NO ERROR!>> |
| Opera 12.15 | Mac | Unhandled Error: Illegal property access |
| Firefox 21.0 | Mac | TypeError: access to strict mode caller function is censored |
| Safari 6.0.4 | Mac | TypeError: Function.caller used to retrieve strict caller |
+-----------------------+-----+--------------------------------------------------------------+
Note: for OS
, Win
= Windows 7, Mac
= Mac OS 10.7.5
My understanding:
- All modern desktop browsers support
use strict
(see Can I use). - The
use strict
is scoped within my function, so everything defined outside its scope is not affected (see this Stack Overflow question).
Question:
So, are all browsers except Chrome wrong? Or is it the other way round? Or is this undefined behaviour so the browsers may choose to implement it in either way?