简体   繁体   English

了解JavaScript代码段

[英]Understanding JavaScript code snippet

This code is from http://twitter.github.com/ 此代码来自http://twitter.github.com/

(function ($, undefined) {
    // more code ...

    $.getJSON("https://api.github.com/orgs/twitter/members?callback=?", function (result) {
        var members = result.data;
        $(function () {
            $("#num-members").text(members.length);
        });
    });

    // more code ...
})(jQuery);

First, things I understand: 首先,我理解的事情:

  • All the code is wrapped in a IIFE 所有代码都包含在IIFE中
  • They are using Github API for getting the members 他们使用Github API来获取成员
  • The URL includes the string '?callback=?' URL包含字符串'?callback =?' so the request is treated as JSONP. 所以请求被视为JSONP。

What I don't understand is: why they are using $(function() ... inside the function that is executed if the request succeeds. 我不明白的是:为什么他们在请求成功时使用$(function() ...在函数内部。

Is this code equivalent? 这段代码是等价的吗?

$(function() {
    $.getJSON("https://api.github.com/orgs/twitter/members?callback=?", function (result) {
        var members = result.data;
         $("#num-members").text(members.length);
    });
});

Maybe I'm wrong but what I think is that the second code snippet waits for the document to be loaded and then request the members ... so there is not parallelism? 也许我错了,但我认为第二个代码片段等待文件加载然后请求成员......所以没有并行性? In the first code snippet the request is done in parallel with the document loading. 在第一个代码片段中,请求与文档加载并行完成。 Please correct me if I'm wrong. 如果我错了,请纠正我。

Maybe I'm wrong but what I think is that the second code snippet waits for the document to be loaded and then request the members 也许我错了,但我认为第二个代码片段等待加载文档然后请求成员

You're not wrong. 你没错。 That is exactly what happens. 这正是发生的事情。 The first snippet is most likely used so the JSONP request can be made/returned while waiting for the DOM to be ready. 最有可能使用第一个片段,因此可以在等待DOM准备好时发出/返回JSONP请求。 They are just making the best use of the time available. 他们只是在充分利用现有的时间。

The chances are the DOM will be ready by the time the AJAX request is complete, but to be on the safe side there is no harm wrapping it in a ready event handler (if the DOM is already ready, jQuery executes the callback immediately). 有可能在AJAX请求完成时DOM已经准备就绪,但为了安全起见,将它包装在就绪事件处理程序中是没有坏处的(如果DOM已经准备就绪,jQuery立即执行回调)。

The $ function, if it is passed a function as its argument (it is a horribly overloaded function), will call that function when the DOM is ready. $函数,如果它传递一个函数作为它的参数(它是一个可怕的重载函数),将在DOM准备好时调用该函数。

Having it there stops the code inside (which tries to modify the DOM) from running before the DOM is complete. 拥有它可以在DOM完成之前停止运行代码(尝试修改DOM)。

If the DOM is already complete before $ is called, then the function will be called immediately. 如果在调用$之前DOM已经完成,那么将立即调用该函数。

Note that the HTTP request sent by getJSON might get a response before or after the browser has finished loading and parsing the DOM of the original document. 请注意, getJSON发送的HTTP请求可能会在浏览器加载并解析原始文档的DOM 之前或之后获得响应。

This allows the request for the data to be sent without waiting for the DOM to be ready while still protecting against premature modification. 这样就可以在不等待DOM准备好的情况下发送数据请求,同时仍然可以防止过早修改。

Is this code equivalent? 这段代码是等价的吗?

No . That waits for the DOM to be ready before it even sends the request for the data. 在它甚至发送数据请求之前等待DOM准备就绪。

As you know, this library makes use of jQuery. 如您所知,此库使用jQuery。 Now, I know how much we all love jQuery , but what if I want to use another library, such as MooTools or Prototype, that redefines the $ character we all know and love? 现在,我知道我们多么喜欢jQuery ,但如果我想使用另一个库,例如MooTools或Prototype,重新定义我们都知道和喜爱的$角色呢? Using the second example you gave, it breaks the code, because the author is trying to use properties of the $ that likely no longer exist because $ != jQuery . 使用你给出的第二个例子,它打破了代码,因为作者试图使用可能不再存在的$属性,因为$ != jQuery

But in the Twitter snippet, $ is a local variable , an argument to the IIFE, and the jQuery object, which is far less likely to be overwritten, is passed in as that argument. 但Twitter的片段, $是一个局部变量 ,参数传递给IIFE和jQuery对象,这是不太可能被覆盖,被传递作为这样的说法。 So now, anyone wishing to use this function/library can go ahead and use it without fear that it will break if they combine it with another library using the $. 所以现在,任何希望使用这个函数/库的人都可以继续使用它,而不用担心如果它们使用$将它与另一个库结合它会破坏它。

In summary, It's all about namespacing , to prevent another library overwriting the $ and breaking your function definition. 总之,它是关于命名空间的 ,它是为了防止另一个库覆盖$并破坏你的函数定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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