在JavaScript开始时启动半冒号的目的是什么? [英] What's the purpose of starting semi colon at beginning of JavaScript?

查看:107
本文介绍了在JavaScript开始时启动半冒号的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

JavaScript库中的前导分号有什么作用?

我注意到很多jQuery插件都以

I have noticed a lot of jQuery plugins start with

;(function(){ /* something in here */ })();

我只是想知道开始的分号是什么,以及最后的空括号。

I just wondered what the beginning semi-colon was for, as well as the empty parentheses at the end.

推荐答案

如果你在一些不正确的'坏'脚本之后包含这个脚本,就会出现分号用分号封闭它的最后一行。在这种情况下,可能会将两个脚本组合在一起并导致代码无效。例如,如果要将多个脚本合并为单个响应。

The semi-colon is there in case you include this script just after some 'bad' script that doesn't properly close off its last line with a semi-colon. In this case it's possible the two scripts would be combined and result in invalid code. For example if you are merging multiple script into a single response.

最后的()正在执行该函数。这创造了一个封闭。私有变量和方法可以在此函数的范围内声明,无法从脚本外部访问。

The () at the end is executing the function. This is creating a closure. Private variables and methods can be declared within the scope of this function that cannot be accessed from outside the script.

这篇关于在JavaScript开始时启动半冒号的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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