简体   繁体   English

为什么JavaScript需要以“;”开头?

[英]Why does the JavaScript need to start with “;”?

I have recently noticed that a lot of JavaScript files on the Web start with a ; 我最近注意到Web上的很多JavaScript文件都以a开头; immediately following the comment section. 紧跟评论部分。

For example, this jQuery plugin's code starts with: 例如, 这个jQuery插件的代码以:

/**
 * jQuery.ScrollTo
 * Copyright (c) 2007-2008 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 9/11/2008                                      
 .... skipping several lines for brevity...
 *
 * @desc Scroll on both axes, to different values
 * @example $('div').scrollTo( { top: 300, left:'+=200' }, { axis:'xy', offset:-20 } );
 */
;(function( $ ){

Why does the file need to start with a ; 为什么文件需要以a开头; ? I see this convention in server-side JavaScript files as well. 我也在服务器端JavaScript文件中看到了这种约定。

What are the advantages and disadvantages of doing this? 这样做的优点和缺点是什么?

I would say since scripts are often concatenated and minified/compressed/sent together there's a chance the last guy had something like: 我会说因为脚本经常连接和缩小/压缩/一起发送,所以最后一个人有可能有这样的事情:

return {
   'var':'value'
}

at the end of the last script without a ; 在最后一个脚本的末尾没有; on the end. 最后。 If you have a ; 如果你有; at the start on yours, it's safe, example: 在你的开始,它是安全的,例如:

return {
   'var':'value'
}
;(function( $ ){ //Safe (still, screw you, last guy!)

return {
   'var':'value'
}
(function( $ ){ //Oh crap, closure open, kaboom!

return {
   'var':'value'
};
;(function( $ ){ //Extra ;, still safe, no harm

I believe (though I am not certain, so please don't pounce on me) that this would ensure any prior statement from a different file is closed. 我相信(虽然我不确定,所以请不要抨击我),这将确保关闭来自不同文件的任何先前声明。 In the worst case, this would be an empty statement, but in the best case it could avoid trying to track down an error in this file when the unfinished statement actually came from above. 在最坏的情况下,这将是一个空语句,但在最好的情况下,当未完成的语句实际来自上面时,它可以避免尝试追踪此文件中的错误。

Consider this example: 考虑这个例子:

function a() {
  /* this is my function a */
}
a()
(function() {
  /* This is my closure */
})()

What will happen is that it will be evaluated like this: 会发生什么,它将被评估如下:

function a() {
  /* this is my function a */
}
a()(function() {})()

So what ever a is returning will be treated as a function an tried to be initialized. 所以什么都a是返回时,将被视为试图初始化函数。

This is mostly to prevent errors when trying to concat multiply files into one file: 这主要是为了防止在尝试将多个文件连接到一个文件时出错:

a.js a.js

function a() {
  /* this is my function a */
}
a()

b.js b.js

(function() {
  /* This is my closure */
})()

If we concat these files together it will cause problems. 如果我们将这些文件连在一起会导致问题。

So therefore remember to put your ; 所以记得把你的; in front of ( and maybe also a few other places. Btw. var a = 1;;;var b = 2;;;;;;;;;var c = a+b; is perfectly valid JavaScript (也许还有一些其他地方.Btw.var var a = 1;;;var b = 2;;;;;;;;;var c = a+b;是完全有效的JavaScript

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM