gpt4 book ai didi

javascript - (function(){})() 的问题

转载 作者:搜寻专家 更新时间:2023-11-01 05:25:02 24 4
gpt4 key购买 nike

<分区>

到目前为止,我已经了解了使用此功能的好处(它是包装吗?)

因此,它几乎就像命名空间一样。假设我们有:

( 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

})();

我有几个关于这种方法的问题

我尝试过的:

  1. 使用 Bing 获取教程(我找到了它们,但其中很多都没有回答我的问题)
  2. 玩将物体传递到体内
  3. 在这里找到答案

但是,我还在用头撞墙

所以问题是:

我见过人们将对象作为参数传递,但什么时候才有意义?例如,它是什么意思?

  1. ( function(window) {


    })(document);
  2. 我在 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
});

第二个问题是:它是如何工作的。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com