本文介绍了JavaScript自我调用功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图理解这个示例代码,第15行的功能是什么,为什么 start(timeout)
? (对不起,我是编程新手)
I'm trying to understand this example code, what is the function of line 15, why start(timeout)
? (Sorry, I'm new to programming)
var schedule = function (timeout, callbackfunction) {
return {
start: function () {
setTimeout(callbackfunction, timeout)
}
};
};
(function () {
var timeout = 1000; // 1 second
var count = 0;
schedule(timeout, function doStuff() {
console.log(++count);
schedule(timeout, doStuff);
}).start(timeout);
})();
// "timeout" and "count" variables
// do not exist in this scope.
推荐答案
函数调度是作为函数执行的。该函数返回一个对象。就像你可以看到 {start ...}
。用返回的对象调用 start
函数。这就是所谓的链接。因此,在设置函数之后执行 start
函数。
The function schedule is executed as a function. That function returns an object. Like you can see with the { start... }
. With the returned object it calls out the start
function. This is called chaining. So the start
function is executed after is set the function.
奇怪的是,超时传递给没有参数的 start
函数。
What is strange is that the timeout is passed to the start
function which has no parameters.
这篇关于JavaScript自我调用功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!