简体   繁体   English

带有jQuery的javascript中的匿名函数

[英]Anonymous function in javascript with jquery

What is the difference between 之间有什么区别
I know here the global jQuery is passed as $ to function, 我知道这里全局jQuery作为$传递给函数,

(function($){

})(jQuery);

and this one 还有这个

$(function(){

})();

The second one is not a common pattern (it will throw a type error), unless you included the invoking parentheses by mistake: 第二个不是常见的模式(它将引发类型错误),除非您错误地包括了调用括号:

(function($){
    // Normal IIFE that happens to pass jQuery in as an argument
})(jQuery);

$(function(){
    // Shorthand DOM-ready event handler
}); // <-- Remove the invoking parentheses

$(function(){ // Backbone code in here }); $(function(){//这里的骨干网代码}); :

This is an alias to jQuery's “DOMReady” function which executes when the DOM is ready to be manipulated by your JavaScript code. 这是jQuery“ DOMReady”函数的别名,该函数在DOM准备由您的JavaScript代码处理时执行。 This allows you to write code that needs the DOM, knowing that the DOM is available and ready to be read, written to, and otherwise modified by your application. 这使您可以编写需要DOM的代码,同时知道该DOM可用并且可以被您的应用程序读取,写入和修改。

This is not a module, though. 但是,这不是一个模块。 This is only a callback function passed in to the DOMReady alias. 这只是传递给DOMReady别名的回调函数。 The major difference between a module and a callback, in this case, is that jQuery waits for the DOM to be ready and then calls the callback function at the appropriate time – all from the context of jQuery – while a module pattern or immediately invoking function executes immediately after it's defined. 在这种情况下,模块和回调之间的主要区别在于,jQuery等待DOM准备就绪,然后在适当的时间(全部来自jQuery的上下文)调用回调函数,而模块模式或立即调用函数定义后立即执行。 In the above examples, the module is receiving jQuery as a parameter, but this is not the same as using jQuery's DOMReady event because the module function is called, passing in jQuery as a parameter, immediately. 在以上示例中,模块正在接收jQuery作为参数,但这与使用jQuery的DOMReady事件不同,因为调用了模块函数,并立即将jQuery作为参数传递。 It does not wait for the DOM to be ready. 它不等待DOM准备就绪。 It executes as soon as the function has been parsed. 函数解析后立即执行。

(function($) { // Backbone code in here })(jQuery);: (function($){//这里的骨干网代码})(jQuery);:

This is an immediately-invoking function expression (FKA “anonymous function”, “self-invoking function”, etc). 这是立即调用的函数表达式(FKA“匿名函数”,“自调用函数”等)。

The implementation of this is a function that is immediately invoked by the calling (jQuery) parenthesis. 此函数的实现是一个由调用(jQuery)括号立即调用的函数。 The purpose of passing jQuery in to the parenthesis is to provide local scoping to the global variable. 将jQuery传递到括号中的目的是为全局变量提供局部范围。 This helps reduce the amount of overhead of looking up the $ variable, and allows better compression / optimization for minifiers in some cases. 这有助于减少查找$变量的开销,并在某些情况下为压缩器提供更好的压缩/优化。

In this case, the function is being used as the JavaScript “module” pattern. 在这种情况下,该函数将用作JavaScript“模块”模式。 Modules in the currently implemented version of JavaScript in most browsers, are not specific constructs like functions. 在大多数浏览器中,当前实现的JavaScript版本中的模块不是特定的构造,例如函数。 Rather, they are a pattern of implementation that use an immediately invoking function to provide scope and privacy around a “module” of related functionality. 相反,它们是一种实现方式,使用立即调用功能围绕相关功能的“模块”提供范围和隐私。 It's common for modules to expose a public API – the “revealing module” pattern – by returning an object from the module's function. 模块通常通过从模块函数中返回一个对象来公开公共API(“公开模块”模式)。 But at times, modules are entirely self-contained and don't provide any external methods to call. 但是有时,模块是完全独立的,不提供任何外部方法来调用。

check this 检查这个

$(function(){

});

This is just the shorthand for the DOM-ready event handler, which is the equivalent of: 这只是DOM就绪事件处理程序的简写,它等效于:

$(document).ready(function(){
    // execution when DOM is loaded...
});

Now over to this: 现在到这里:

(function($){
    // code here
})(jQuery);

This code will not be executed on DOM ready, but it will be executed directly. 该代码不会在DOM准备就绪时执行,但会直接执行。 What the advantage is to pass jQuery as a parameter into the function, is to avoid collisions with the usage of the dollar symbol ( $ ), as multiple libraries use it as a shorthand reference. 将jQuery作为参数传递给函数的好处是避免与美元符号( $ )的使用发生冲突,因为多个库都将其用作速记引用。 Everything inside the function can safely use $ , as this is being passed in as a reference to jQuery . 函数内部的所有内容都可以安全地使用$ ,因为它将作为对jQuery的引用传递。

Read more about conflicts on the $ symbol: jQuery noConflict 阅读有关$符号上的冲突的更多信息: jQuery noConflict

If you combine the two code snippets, you get a nice and solid setup: 如果将这两个代码段结合在一起,则会得到一个不错而坚实的设置:

// $ reference is unsafe here in the global scope if you use multiple libraries   

(function($){
    // $ is a reference to jQuery here, passed in as argument

    $(function(){
        // executed on dom-ready
    });

})(jQuery);

PS : Because function in JavaScript can be both a statement or an expression, depending upon context, most people add extra parenthesis around it to force it to be an expression. PS :由于JavaScript中的function既可以是语句也可以是表达式,根据上下文,大多数人会在其周围添加额外的括号以强制将其变为表达式。

The first snipset will execute the "function($){...}" as js parser encounter it, creating a kind of private context inside it where $ argument var will point to jQuery because it is passed as argument "(jQuery)" (Useful if you want to avoid any collision with a previously declared $ var which would reference something else than the jQuery object) 第一个片段将在js解析器遇到时执行“ function($){...}”,并在其中创建一种私有上下文,其中$参数var将指向jQuery,因为它作为参数“(jQuery)”传递(如果要避免与先前声明的$ var发生任何冲突,该变量将引用除jQuery对象之外的其他内容,则很有用)

The second one looks like JQuery.ready function call but with a syntax error. 第二个看起来像JQuery.ready函数调用,但是带有语法错误。 There is two way for writing it actualy 实际上有两种写法

$(document).ready(function(){
     /* DOM has loaded */
});

$(function(){
    /* DOM has loaded */
});
(function ($)

})(jQuery); 

it a function being defined and then immediately called, with the JQuery object passed in as an argument. 它定义了一个函数,然后立即调用该函数,并将JQuery对象作为参数传入。 The $ is a reference to JQuery which you can then use inside the function. $是对JQuery的引用,您可以在函数内部使用它。 It's equivalent to this: 等效于:

var Test = function ($){};
Test(jQuery);

and this: 和这个:

$(function (){

});

is a call to JQuery, passing in a function which it should execute once the document has finished loading. 是对JQuery的调用,传递了一个函数,该函数应在文档加载完成后立即执行。

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

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