Possible Duplicate:
What does the leading semicolon in JavaScript libraries do?

I have noticed a lot of jQuery plugins start with

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

我只是想知道开头的分号是干什么的,以及结尾的空括号.

推荐答案

分号就在那里,以防你在某个"坏"脚本没有正确地用分号结束最后一行之后包含这个脚本.在这种情况下,这两个脚本可能会合并,并导致无效代码.例如,如果要将多个脚本合并到一个响应中.

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.

Jquery相关问答推荐

我在自定义 WordPress 插件中使用 ajax 时收到错误请求

将日期时间从 javascript 传递给 c# (Controller)

在 Bootstrap 3 中向工具提示添加换行符

jQuery $(document).ready () 触发两次

用按钮切换显示/隐藏div?

从 JQuery.ajax 成功数据中解析 JSON

使用 jquery 在 radio 上单击或更改事件

如何清除/重置 jQuery UI Datepicker 日历上的选定日期?

Bootstrap 折叠动画不流畅

jQuery Uncaught TypeError: 对象 [object Window] 的属性$不是函数

jQuery 日期 Select 器 - 禁用过go 的日期

bootstrap 模式:关闭当前,打开新

当 iframe 在 jQuery 中完成加载时,如何触发事件?

jQuery .hasClass() 与 .is()

jquery,id内类的 Select 器

无法更新数据属性值

如何在 jQuery 中 Select this中的元素?

设置 JQuery event.preventDefault() 时绕过 window.open 上的弹出窗口阻止程序

IE8 和 JQuery 的 trim()

如何使用 jquery 使下拉列表只读?