【发布时间】:2011-09-02 23:14:34
【问题描述】:
所以我看到了一个函数,坦率地说,它的简单性非常漂亮,因为它允许您在匿名函数中找到全局对象(取决于当时的环境可能不是 window );但是,当您抛出 javascripts 的“使用严格”时;由于对关键字“this”的评估发生了变化,它会崩溃。有几种方法可以做到这一点?
(function () {
var win = function () {
return (function () {
return this;
}());
};
//win now points to the global object no matter where it is called.
}());
现在,如果在“使用严格”的上下文中调用它们,我们将失去所描述的功能,是否有任何等效的可以在 ES5 严格模式下完成?
供参考
(function () {
"use strict"
//code here is in strict mode
}())
【问题讨论】: