我在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";的顶部,但有没有办法实现相反的目的?
所以我看到一个函数,它的简单性非常坦率,因为它允许你在匿名函数中找到全局对象(当时依赖于环境可能不是窗口); 但是当你抛出javascripts的"use strict"时; 由于关键字'this'的变化评估,它会崩溃.有几种方法可以实现这一目标?
(function () {
var win = function () {
return (function () {
return this;
}());
};
//win now points to the global object no matter where it is called.
}());
Run Code Online (Sandbox Code Playgroud)
现在,如果在"use strict"的上下文中调用它们,我们将失去所描述的功能,是否有任何可以在ES5严格模式下完成的等效操作?
以供参考
(function () {
"use strict"
//code here is in strict mode
}())
Run Code Online (Sandbox Code Playgroud)