¿Hay alguna manera de comprobar si se aplica el modo estricto?


Está allí de todos modos para comprobar si el modo estricto 'use strict' se aplica , y queremos ejecutar código diferente para el modo estricto y otro código para el modo no estricto. Buscando una función como isStrictMode();//boolean

Author: Bhargav Rao, 2012-05-07

6 answers

El hecho de que this dentro de una función llamada en el contexto global no apunte al objeto global se puede usar para detectar el modo estricto:

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
 68
Author: ThiefMaster,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-05-07 10:08:11
function isStrictMode() {
    try{var o={p:1,p:2};}catch(E){return true;}
    return false;
}

Parece que ya tienes una respuesta. Pero ya escribí un código. Así que aquí

 21
Author: Thalaivar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-23 08:28:18

Prefiero algo que no use excepciones y funcione en cualquier contexto, no solo global:

var mode = (eval("var __temp = null"), (typeof __temp === "undefined")) ? 
    "strict": 
    "non-strict";

Utiliza el hecho de que el modo estricto eval no introduce una nueva variable en el contexto externo.

 18
Author: noseratio,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-09-20 12:26:27

Yep, this es 'undefined' dentro de un método global cuando estás en modo estricto.

function isStrictMode() {
    return (typeof this == 'undefined');
}
 9
Author: Mehdi Golchin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-10-06 09:34:58

Forma más elegante: si" this " es objeto, conviértalo a verdadero

"use strict"

var strict = ( function () { return !!!this } ) ()

if ( strict ) {
    console.log ( "strict mode enabled, strict is " + strict )
} else {
    console.log ( "strict mode not defined, strict is " + strict )
}
 1
Author: ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-08-30 10:42:24

Otra solución puede aprovechar el hecho de que en modo estricto, las variables declaradas en eval no se exponen en el ámbito exterior

function isStrict() {
    var x=true;
    eval("var x=false");
    return x;
}
 0
Author: Yaron U.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-06-23 10:18:04