简体   繁体   English

$在“ jQuery(function($){…}}”中的符号如何表示jQuery?

[英]how does $ sign in “jQuery(function($){…}” refer to jQuery?

below, it says $ sign refers to jQuery. 在下面,它说$符号指的是jQuery。 i need help understand how? 我需要帮助,了解如何?

jQuery(function($){
    // Here `$` refers to jQuery
});

When you pass a function to jQuery like this: 当您像这样将函数传递给jQuery时:

jQuery(function() {
    ...
});

It's the same as using jQuery's "document ready" handler: 与使用jQuery的“文档就绪”处理程序相同:

jQuery(document).ready(function() {
    ...
});

however the argument passed to that function is actually the global jQuery object itself - it's just that most such handlers never use that parameter. 但是,传递给该函数的参数实际上是全局jQuery对象本身-只是大多数此类处理程序从未使用该参数。 Hence the real signature is this: 因此, 真正的签名是这样的:

jQuery(document).ready(function($) {
    ...
});

so, within that function $ is a local alias to the global jQuery object. 因此,在该函数中$是全局jQuery对象的本地别名。 The variable name could be anything you wanted, but $ is a popular alias for jQuery , and the default global alias unless you call jQuery.noConflict() . 变量名可以是您想要的任何名称,但是$jQuery的常用别名,并且是默认的全局别名,除非您调用jQuery.noConflict()

This is described in more detail in the paragraph " Aliasing the jQuery Namespace " at http://api.jquery.com/ready/ http://api.jquery.com/ready/的别名化jQuery命名空间 ”段落中对此进行了更详细的描述。

That function is a so called allonymous function where jQuery runs that allonymous function with the first parameter this. 该函数是所谓的同义函数,其中jQuery使用第一个参数this运行同义函数。

So just for understanding it makes something like this: 因此,仅出于理解,它会产生以下内容:

function jQuery(xx) {
    xx(this);
}

Just an example for show you how is it possibile 只是一个示例,向您展示它如何可能

function externalLibrary(b) {
    if(typeof b === 'function') {
        b(externalLibrary);
    }else {
        // other stuff
    }

}

var myFunc = function(aliasOfExternalLibrary) {
    // here aliasOfExternalLibrary is a reference to externalLibrary
}
externalLibrary( myFunc );

jQuery do the same (in one more complex system) jQuery 做同样的事情 (在一个更复杂的系统中)

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

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