简体   繁体   English

有人可以解释这个Javascript代码吗?

[英]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: 让我解释一下我不了解的内容:

  1. the call: CheckDependency.Deferred.execute(foo); 调用: CheckDependency.Deferred.execute(foo);
    • why the "Deferred" keyword? 为什么使用“延迟”关键字?
    • 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. 为什么它明显运行计时器功能。 why could it not simply have that code after the timer() since it keeps looping there and then return jquery? 为什么不能在timer()之后简单地包含该代码,因为它一直在那儿循环然后返回jquery?
  2. Speaking of return. 说到回报。 Why is there a method in there? 为什么那里有一种方法? 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM