【问题标题】:What does this JavaScript snippet mean? [duplicate]这个 JavaScript 片段是什么意思? [复制]
【发布时间】:2011-07-21 04:55:42
【问题描述】:

我以前没有遇到过这种语法。这是什么意思?它与什么技术有关?

(function(fun) { 

})(myFunkyAlert);

【问题讨论】:

    标签: javascript function grammar


    【解决方案1】:

    这是一个匿名函数,一旦声明就会运行。它的参数是myFunkyAlert,在函数内部它会被引用为fun变量。

    我们通常写这样一个函数的原因是为了避免冲突,因为作用域。

    例子:

    var myFunkyAlert = "The funky alert";
    
    (function(fun) { 
       alert(fun);
    })(myFunkyAlert);
    

    这将导致一个带有“The funky alert”消息的警报。

    【讨论】:

      【解决方案2】:

      您正在定义一个匿名函数,然后使用 myFunkyAlert 作为参数调用它。

      【讨论】:

      • 举例说明这个习语什么时候有用可能会有所帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-05
      • 1970-01-01
      • 2012-03-14
      • 1970-01-01
      • 2015-03-12
      相关资源
      最近更新 更多