Is there any way to check if strict mode is enforced?
function isStrictMode() {
try{var o={p:1,p:2};}catch(E){return true;}
return false;
}
Looks like you already got an answer. But I already wrote some code. So here
The fact that this
inside a function called in the global context will not point to the global object can be used to detect strict mode:
var isStrict = (function() { return !this; })();
Demo:
> echo '"use strict"; var isStrict = (function() { return !this; })(); console.log(isStrict);' | node
true
> echo 'var isStrict = (function() { return !this; })(); console.log(isStrict);' | node
false
I prefer something that doesn't use exceptions and works in any context, not only global one:
var mode = (eval("var __temp = null"), (typeof __temp === "undefined")) ?
"strict":
"non-strict";
It uses the fact the in strict mode eval
doesn't introduce a new variable into the outer context.