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

查看:174
本文介绍了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.

它有任何其他功能吗?

推荐答案

它允许您安全地将几个JS文件连接成一,作为一个HTTP请求更快地提供服务。

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

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

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