我可以为特定功能禁用 ECMAscript 严格模式吗?
我在 MDC 或 ECMAscript 规范上找不到任何有关我的问题的信息。可能有人知道一种更“hacky”的方法来解决这个问题。
我正在对我的环境中的每个 javascript 文件调用“use strict”
。我的所有文件都是这样开始的
(function(win, doc, undef) {
"use strict";
// code & functions
}(window, window.document));
现在,我有一个处理错误的自定义函数。该函数使用 .caller
属性来提供上下文堆栈跟踪。看起来像这样:
var chain = (function() {
var _parent = _error,
_ret = '';
while( _parent.caller ) {
_ret += ' -> ' + _parent.caller.name;
_parent = _parent.caller;
}
return _ret;
}());
但是,当然,在严格模式下 .caller
是一个不可删除的 prop,在检索时会抛出异常。所以我的问题是,有人知道如何禁用更严格的“功能方面”吗?
"use strict";
在调用后被所有函数继承。现在我们可以通过在这些函数的顶部调用 "use strict";
来在特定函数中使用严格模式,但是有没有办法实现相反的效果呢?
I don't find anything about my question here on MDC or the ECMAscript specifications. Probably somebody knows a more 'hacky' way to solve this.
I'm calling "use strict"
on every javascript file in my environment. All my files start like this
(function(win, doc, undef) {
"use strict";
// code & functions
}(window, window.document));
Now, I have a custom function which handles errors. That functions uses the .caller
property to provide a context stack trace. Looks like this:
var chain = (function() {
var _parent = _error,
_ret = '';
while( _parent.caller ) {
_ret += ' -> ' + _parent.caller.name;
_parent = _parent.caller;
}
return _ret;
}());
But of course, in strict mode .caller
is a non-deletable prop which throws when retrieved. So my question is, is anybody aware of way to disable strict more "function-wise" ?
"use strict";
is inherited by all functions after it was called. Now we have the possibilty to just use strict mode in specific functions by just calling "use strict";
at the top of those, but is there a way to achieve the opposite ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,您不能禁用每个函数的严格模式。
重要的是要理解严格模式在词法上上起作用;意思是——它影响函数声明,而不是执行。严格代码中声明的任何函数本身都会成为严格函数。但并非任何从严格代码中调用的函数都一定是严格的:
注意我们如何在严格代码之外定义函数,然后将其传递到严格的函数中。
您可以在示例中执行类似的操作 - 拥有一个具有“草率”函数的对象,然后将该对象传递给该严格的立即调用函数。当然,如果“草率”函数需要引用主包装函数中的变量,那么这将不起作用。
另请注意,间接评估(由其他人建议)获胜在这里真的没有帮助。它所做的只是在全局上下文中执行代码。如果您尝试调用本地定义的函数,间接 eval 甚至找不到它:
这种关于全局 eval 的混淆可能来自这样一个事实:全局 eval 可用于从严格模式(这不是“严格模式”)内访问全局对象。不再可以通过
this
简单地访问):但是回到问题......
你可以通过
Function
构造函数来欺骗并声明一个新函数 - 这发生在 不继承严格性,但这将依赖于(非标准)函数反编译,并且您将失去引用外部变量的能力。请注意,FF4+ 似乎不符合规范(据我所知),并且错误地将通过
Function
创建的函数标记为严格。在其他严格模式支持实现(例如 Chrome 12+)中不会发生这种情况、IE10、WebKit)。No, you can't disable strict mode per function.
It's important to understand that strict mode works lexically; meaning — it affects function declaration, not execution. Any function declared within strict code becomes a strict function itself. But not any function called from within strict code is necessarily strict:
Notice how we can define function outside of strict code and then pass it into the function that's strict.
You can do something similar in your example — have an object with "sloppy" functions, then pass that object to that strict immediately invoked function. Of course, that won't work if "sloppy" functions need to reference variables from within main wrapper function.
Also note that indirect eval — suggested by someone else — won't really help here. All it does is execute code in global context. If you try to call a function that's defined locally, indirect eval won't even find it:
This confusion about global eval probably comes from the fact that global eval can be used to get access to global object from within strict mode (which isn't simply accessible via
this
anymore):But back to the question...
You can kind of cheat and declare a new function via
Function
constructor — which happens to not inherit strictness, but that would rely on (non-standard) function decompilation and you would lose ability to reference outer variables.Note that FF4+ seems to disagree with spec (from what I can tell) and incorrectly marks function created via
Function
as strict. This doesn't happen in other strict-mode-supporting implementations (like Chrome 12+, IE10, WebKit).(来自 http://javascriptweblog.wordpress.com/2011/05/ 03/javascript-严格模式/)
因此,如果您在不同的文件中设置错误方法,而不使用严格模式,然后将它们作为参数传递,如下所示:
...它应该可以工作。
(From http://javascriptweblog.wordpress.com/2011/05/03/javascript-strict-mode/)
So if you setup the error methods in a different file, without strict mode, and then pass them as a parameter, like this:
...it should work.
另一种选择就是简单地这样做
An alternative is simply doing this