我可以为特定功能禁用ECMAscript严格模式吗?

jAn*_*ndy 70 javascript ecma262 ecmascript-5 strict-mode

我在MDC或ECMAscript规范中没有找到关于我的问题的任何内容.可能有人知道一种更"笨拙"的方法来解决这个问题.

我正在调用"use strict"我环境中的每个javascript文件.我的所有文件都是这样开始的

(function(win, doc, undef) {
    "use strict";

    // code & functions
}(window, window.document));
Run Code Online (Sandbox Code Playgroud)

现在,我有一个处理错误的自定义函数.该函数使用该.caller属性来提供上下文堆栈跟踪.看起来像这样:

var chain = (function() {
    var _parent = _error,
        _ret = '';

    while( _parent.caller ) {
        _ret += ' -> ' + _parent.caller.name;
        _parent = _parent.caller;
    }

    return _ret;
}());
Run Code Online (Sandbox Code Playgroud)

但是,当然,在严格模式下.caller是一个不可删除的道具,在检索时抛出.所以我的问题是,是否有人知道如何禁用更严格的"功能明智"?

"use strict";在被调用之后被所有函数继承.现在我们有可能在特定函数中使用严格模式,只需调用它们"use strict";的顶部,但有没有办法实现相反的目的?

kan*_*gax 80

不,您无法为每个功能禁用严格模式.

重要的是要理解严格模式在词汇上有效 ; 意义 - 它影响函数声明,而不是执行.在严格代码中声明的任何函数本身都成为严格函数.但是,在严格的代码中调用的任何函数都不一定是严格的:

(function(sloppy) {
  "use strict";

   function strict() {
     // this function is strict, as it is _declared_ within strict code
   }

   strict();
   sloppy();

})(sloppy);

function sloppy(){
  // this function is not strict as it is _declared outside_ of strict code
}
Run Code Online (Sandbox Code Playgroud)

注意我们如何在严格代码之外定义函数,然后将其传递给严格的函数.

您可以在您的示例中执行类似的操作 - 使用"sloppy"函数的对象,然后将该对象传递给严格的立即调用函数.当然,如果"草率"函数需要从主包装函数中引用变量,那么这将不起作用.

另请注意,间接评估 - 由其他人建议 - 在这里不会真正有用.它所做的只是在全局上下文中执行代码.如果您尝试调用本地定义的函数,间接eval甚至不会找到它:

(function(){
  "use strict";

  function whichDoesSomethingNaughty(){ /* ... */ }

  // ReferenceError as function is not globally accessible
  // and indirect eval obviously tries to "find" it in global scope
  (1,eval)('whichDoesSomethingNaughty')();

})();
Run Code Online (Sandbox Code Playgroud)

关于全局评估的这种混淆可能来自这样一个事实:全局评估可用于从严格模式(不能简单地访问this)访问全局对象:

(function(){
  "use strict";

  this; // undefined
  (1,eval)('this'); // global object
})();
Run Code Online (Sandbox Code Playgroud)

但回到这个问题......

您可以通过Function构造函数来欺骗并声明一个新函数- 这恰好不会继承严格性,但这将依赖于(非标准)函数反编译,您将失去引用外部变量的能力.

(function(){
  "use strict";

  function strict(){ /* ... */ }

  // compile new function from the string representation of another one
  var sneaky = Function('return (' + strict + ')()');

  sneaky();
})();
Run Code Online (Sandbox Code Playgroud)

请注意,FF4 +似乎不同意规范(从我可以告诉的)并错误地标记通过Function严格创建的功能.在其他严格模式支持的实现(如Chrome 12 +,IE10,WebKit)中不会发生这种情况.


Nil*_*oct 5

(来自http://javascriptweblog.wordpress.com/2011/05/03/javascript-strict-mode/

(...) 在严格函数体内调用的非严格函数上不强制执行严格模式(因为它们作为参数传递或使用callor 调用apply)。

因此,如果您在不同的文件中设置错误方法,而不使用严格模式,然后将它们作为参数传递,如下所示:

var test = function(fn) {
  'use strict';
  fn();
}

var deleteNonConfigurable = function () {
  var obj = {};
  Object.defineProperty(obj, "name", {
    configurable: false
  });
  delete obj.name; //will throw TypeError in Strict Mode
}

test(deleteNonConfigurable); //no error (Strict Mode not enforced)
Run Code Online (Sandbox Code Playgroud)

...它应该有效。