【发布时间】:2015-12-02 09:42:30
【问题描述】:
我花了很多时间来理解 this、func.bind(this)、func.bind(exports) 和 function() { that.func(); }。但是,我无法理解的是,当模块中没有为它定义导出时,以下 setTimeout 如何能够在私有范围内查看和访问 doStuff()?
window.TestModule = (function() {
function init() {
document.getElementById('testbt').onclick = test;
}
function test() {
setTimeout(function() {
alert(this); //window
doStuff(); //works! wow!
}, 250);
}
function doStuff() {
//do stuff
}
return {
init: init
};
}());
TestModule.init();
【问题讨论】:
标签: javascript module scope closures settimeout