查看 Leaflet API 中的代码
迷失在争论和闭包中。
我的第二个参数日志输出是一个空数组。第一个日志不应相同。
limitExecByInterval: function (fn, time, context) {
var lock, execOnUnlock;
// Log output 1
console.log(arguments);//[foo(), 10000, Window a.html]
return function wrapperFn() {
var args = arguments;
if (lock) {
execOnUnlock = true;
return;
}
lock = true;
setTimeout(function () {
lock = false;
if (execOnUnlock) {
wrapperFn.apply(context, args);
execOnUnlock = false;
}
}, time);
//Log output 2
console.log(args) //[]
fn.apply(context, args);
};
},
最佳答案
Shouldn't be the same at the first log.
没有。每个函数都有自己的arguments
对象。第二个 console.log
调用将记录传递给 wrapperFn
的参数,它在您的测试中似乎没有收到任何参数。
关于javascript - 带有函数参数数组的闭包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27815336/