Monday, May 20, 2024
93
rated 0 times [  100] [ 7]  / answers: 1 / hits: 22087  / 12 Years ago, mon, may 7, 2012, 12:00:00

Is there anyway to check if strict mode 'use strict' is enforced , and we want to execute different code for strict mode and other code for non-strict mode.
Looking for function like isStrictMode();//boolean


More From » ecmascript-5

 Answers
260

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

[#85739] Sunday, May 6, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
koltenb

Total Points: 276
Total Questions: 92
Total Answers: 101

Location: North Korea
Member since Fri, Nov 4, 2022
2 Years ago
;