JavaScript 库中的前导分号有什么作用? [英] What does the leading semicolon in JavaScript libraries do?

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

问题描述

在几个 JavaScript 库中,我一开始就看到了这个符号:

In several JavaScript libraries I saw this notation at the very beginning:

/**
 * Library XYZ
 */
;(function () {
  // ... and so on

虽然我对立即执行的函数"语法非常满意

While I'm perfectly comfortable with the "immediately executed function" syntax

(function(){...})()

我想知道前导分号是做什么用的.我能想到的就是它是一种保险.也就是说,如果该库被嵌入到其他有缺陷的代码中,它就会充当最后一条语句最迟在此处结束"的一种减速带.

I was wondering what the leading semicolon is for. All I could come up with is that it is an insurance. That is, if the library is embedded in other, buggy code, it serves as an "the last statement ends here at the latest" kind of speed bump.

它还有其他功能吗?

推荐答案

它允许您将多个 JavaScript 文件安全地连接为一个,以作为一个 HTTP 请求更快地提供服务.

It allows you to safely concatenate several JavaScript files into one, to serve it quicker as one HTTP request.

这篇关于JavaScript 库中的前导分号有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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