Javascript:为什么jQuery这样做:(function(){...});,它是如何工作的? [英] Javascript: why does jQuery do this: (function(){ ...});, and how does it work?

查看:110
本文介绍了Javascript:为什么jQuery这样做:(function(){...});,它是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:我认为
我看到的jQuery源代码是这样的:

I THOUGHT The jQuery source I was looking at did something like this:

(function(){
    var bunchOfVariables = 7;
    jQuery = " ....."; 
    //....
});

我错了。忽略这个问题。

I was wrong about that. Ignore this question.

我不明白这是做什么的。有人可以解释一下吗?

I don't understand what that does. Can someone please explain it?

这是 jQuery-1.3.2.js

This is the very first line in jQuery-1.3.2.js.

它似乎定义了一个匿名函数,而不是执行它。功能在哪里?怎么运行?

It appears to define an anonymous function, and NOT execute it. Where does the function go? How does it get run?

如果我在测试脚本中使用这样的代码,它永远不会被调用。另一方面,如果我用open-close paren跟随它,那么它会被调用:

If I use code like that in a test script, it never gets called. On the other hand, if I follow it with open-close paren, then it gets called :

// never gets called
(function(){
    say("hello");
});
// gets called
(function(){
    say("buon giorno");
})();


推荐答案

jQuery源代码的最后一行是

The very last line of the jQuery source is

})();

括号表示正在调用函数。

The parentheses mean that the function is being called.

这篇关于Javascript:为什么jQuery这样做:(function(){...});,它是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆