<分区>
到目前为止,我已经了解了使用此功能的好处(它是包装吗?)
因此,它几乎就像命名空间一样。 假设我们有:
( function() {
function foo(){
alert(true);
}
foo(); //alerts true
})();
( function() {
function foo(){ //the same title of the function as above
alert("another call of foo");
}
foo(); //alerts, ok.
})();
我还注意到它可以访问公共(public)变量,如下所示:
var __foo__ = 'bar';
( function() {
alert(__foo__); //alerts bar
})();
我有几个关于这种方法的问题
我尝试过的:
- 使用 Bing 获取教程(我找到了它们,但其中很多都没有回答我的问题)
- 玩将物体传递到体内
- 在这里找到答案
但是,我还在用头撞墙
所以问题是:
我见过人们将对象作为参数传递,但什么时候才有意义? 例如,它是什么意思?
-
( function(window) { })(document);
我在 Jquery UI Lib 中看到了这样的东西
( function($) { //some code of widget goes here })(Jquery);
这使得内部代码在函数外可见,对吧? (不确定)为什么,这是因为 我们可以访问对象(假设我们有“模态”小部件),只需调用它,
喜欢:
$(function(){
$("#some_div").modal(); //here it's object the we got from the function
});
第二个问题是:它是如何工作的。