下面这段代码中括号的作用是什么? [英] What's the role of the parentheses in the following piece of code?

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

问题描述

这是Google Analytics的追踪程式码:

This is the tracking code for Google Analytics:

var _gaq = _gaq || [];
_gaq.push(["_setAccount", "UA-256257-21"]);
_gaq.push(["_trackPageview"]);

(function() {
var ga = document.createElement("script"); ga.type = "text/javascript"; ga.async = true;
ga.src = ("https:" == document.location.protocol ? "https://ssl" : "http://www") + ".google-analytics.com/ga.js";
var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(ga, s);
})();

您可以看到该函数在圆括号内。

You can see that the function is inside parentheses.

为什么你认为是这样?

推荐答案

这是一个匿名函数,可以立即定义和调用。它不能从外部调用,因为它没有名字。所有内部的变量将被限定为匿名函数。这可以用来在全局范围内进行一些处理,而无需添加新成员。

It is an anonymous function that is defined and invoked immediately. It cannot be invoked from the outside as it has no name. All the variables inside will be scoped to the anonymous function. This could be used to do some processing on the global scope without adding new members to it.

这篇关于下面这段代码中括号的作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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