0
我有库代码做了一个简单的函数分配。该功能可以从全局范围访问。为什么在全局范围内使用“严格的”命名空间中的break函数分配?
当我在文件的开头添加"use strict"
时,我得到一个错误TypeError: a is undefined
,分配给a.b
。
"use strict"; /* Remove this and 'a' is defined */
(function() {
var a = this;
a.b = function() {
document.getElementById('test').innerHTML = 'abc';
};
})();
b();
<div id="test"></div>
为什么我得到这个错误考虑var a
是上一行声明?
这一点已经在这里找到答案。 http://stackoverflow.com/questions/7290086/javascript-use-strict-and-nicks-find-global-function –
是的,'a'被声明,但它被分配给'this',它在严格模式下是未定义的 – hindmost