[英]Can someone explain this Javascript code?
I am trying to understand how this code works. 我试图了解此代码的工作方式。 I finally figured out it is a loop.
我终于发现这是一个循环。 It is not a "while" or "for" loop, but it is a loop nonetheless by virtue of calling itself I think (please correct me if I am wrong).
它不是一个“ while”或“ for”循环,但是仍然是一个循环,因为我认为自己调用了它(如果我错了,请纠正我)。
I understand it's main function: to pass JQuery when it is loaded to my 'foo' function, when ever jQuery has loaded. 我了解它的主要功能:加载jQuery时,将JQuery加载到我的'foo'函数时传递。 To do that it checks for jQuery in
Window
and if not there it resets the timer()
. 为此,它将在
Window
检查jQuery,如果不存在,它将重置timer()
。 That is the loop. 那就是循环。 I get that.
我明白了。
Let me explain what I do not understand: 让我解释一下我不了解的内容:
CheckDependency.Deferred.execute(foo);
CheckDependency.Deferred.execute(foo);
execute
baffles me: I expect that if I call CheckDependency.Deferred.execute
that it would only execute that method. execute
让我感到困惑:我希望如果我调用CheckDependency.Deferred.execute
,它将只会执行该方法。 Why does it obviously run the timer function. CheckDependency.Deferred.execute(foo);
is as crazy to me as CheckDependency.Deferred.RETURN.execute(foo);
CheckDependency.Deferred.RETURN.execute(foo);
一样疯狂CheckDependency.Deferred.RETURN.execute(foo);
(or some similar crazy statement) I am fairly new to JavaScript (from PHP). 我对JavaScript(来自PHP)很陌生。 Here the code:
这里的代码:
function foo(){ console.log('jQuery found!');
}
var CheckDependency = CheckDependency || { };
CheckDependency.Deferred = function ()
{
var functions = [];
var timer = function() {
if (window.jQuery) {/* && window.jQuery.ui*/
while (functions.length) {
functions.shift()(window.jQuery);
}
} else {
window.setTimeout(timer, 250);
}
};
timer();
return {
execute: function(onJQueryReady)
{
if (window.jQuery) { // && window.jQuery.ui
onJQueryReady(window.jQuery);
} else {
functions.push(onJQueryReady);
}
}
};
}();
CheckDependency.Deferred.execute(foo);
Let me start by saying I'm not a javascript expert, but I dabble :) I'll take a stab at describing what is going on here. 首先,我说自己不是JavaScript专家,但我还是涉猎:)我将描述一下这里发生的事情。
First, This creates a new object called "CheckDependency". 首先,这将创建一个名为“ CheckDependency”的新对象。
var CheckDependency = CheckDependency || { };
Next, it runs an anonymous function, and stores the result in CheckDependency.Deferred. 接下来,它运行一个匿名函数,并将结果存储在CheckDependency.Deferred中。
CheckDependency.Deferred = function ()
{
.
.
.
.
}()
The anonymous function runs the following code: 匿名函数运行以下代码:
var functions = [];
var timer = function() {
if (window.jQuery) {/* && window.jQuery.ui*/
while (functions.length) {
functions.shift()(window.jQuery);
}
} else {
window.setTimeout(timer, 250);
}
};
timer();
The last part of the function code returns a new function execute
, which gives CheckDependency.Deferred
a function execute
. 函数代码的最后一部分返回一个新的函数
execute
,该函数赋予CheckDependency.Deferred
一个函数execute
。
return {
execute: function(onJQueryReady)
{
if (window.jQuery) { // && window.jQuery.ui
onJQueryReady(window.jQuery);
} else {
functions.push(onJQueryReady);
}
}
};
Finally, this new function is called 最后,这个新功能称为
CheckDependency.Deferred.execute(foo);
The final result of this is that the code starts a background timer that calls itself until window.jQuery
is true - which means jQuery
is loaded. 最终结果是代码启动了一个后台计时器,该计时器将自行调用直到
window.jQuery
为true为止-这意味着jQuery
已加载。 Then, the function passed to execute
is passed into this loop and so will once jQuery
is available, the original function passed to "execute" will be called with the instance of window.jQuery
. 然后,传递给
execute
的函数将传递到此循环,因此一旦jQuery
可用,传递给“ execute”的原始函数将使用window.jQuery
实例进行window.jQuery
。
I hope I did this justice, and I hope my answer helps! 我希望我做到了这一正义,也希望我的回答会有所帮助! Please let me know if you have any question.
如果您有任何疑问,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.