这段JavaScript代码片段是什么意思? [英] What does this JavaScript snippet mean?

查看:156
本文介绍了这段JavaScript代码片段是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前没有遇到过这种类型的语法。这是什么意思?

I have not met this type of grammar before. What does it mean? To what technique is it related?

(function(fun) { 

})(myFunkyAlert);


推荐答案

这是一个匿名函数,它被宣布。它的参数是 myFunkyAlert ,在函数内部它将被引用为 fun 变量。

This is an anonymous function that will run as soon as it is declared. Its parameter is myFunkyAlert and inside the function it will be referenced as the fun variable.

我们通常编写这样一个函数的原因是为了避免由于范围限制导致的冲突。

The reason we usually write a function like that is to avoid conflicts, due to scoping.

示例:

Example:

var myFunkyAlert = "The funky alert";

(function(fun) { 
   alert(fun);
})(myFunkyAlert);

这将产生一条提示信息The funky alert。

This will result in an alert with the message "The funky alert".

这篇关于这段JavaScript代码片段是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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