奇怪的JavaScript语法是这样的:(function(){// code})();? [英] Strange JavaScript syntax like this: (function(){//code}) ();?

查看:68
本文介绍了奇怪的JavaScript语法是这样的:(function(){// code})();?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下JavaScript代表什么意思?为什么函数里面嵌入()?

What does the below JavaScript mean? Why is the function embedded inside ()?

(function() {
    var b = 3;
    a += b;
}) ();


推荐答案

它的功能相当于执行下面的操作:

It's functionaly equivalent to doing something like:

var myFunc = function(){
    var b = 3;
    a += b;
};

myFunc();

它包含了圆括号(和尾部),以便函数立即被调用。正如其他人所说,这个概念被称为匿名功能

It's got the parenthesis around it (and trailing) so that the function is called immediately. As others have said, the concept is called an anonymous function.

这篇关于奇怪的JavaScript语法是这样的:(function(){// code})();?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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