admin管理员组文章数量:1133893
I have recently noticed that a lot of JavaScript files on the Web start with a ;
immediately following the comment section.
For example, this jQuery plugin's code starts with:
/**
* jQuery.ScrollTo
* Copyright (c) 2007-2008 Ariel Flesler - aflesler(at)gmail(dot)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 ;
? I see this convention in server-side JavaScript files as well.
What are the advantages and disadvantages of doing this?
I have recently noticed that a lot of JavaScript files on the Web start with a ;
immediately following the comment section.
For example, this jQuery plugin's code starts with:
/**
* 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 ;
? I see this convention in server-side JavaScript files as well.
What are the advantages and disadvantages of doing this?
Share Improve this question edited Jun 23, 2012 at 18:29 ЯegDwight 25.2k10 gold badges46 silver badges52 bronze badges asked Mar 20, 2010 at 1:33 TK.TK. 28.1k20 gold badges66 silver badges72 bronze badges3 Answers
Reset to default 352I 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.
This is mostly to prevent errors when trying to concat multiply files into one file:
a.js
function a() {
/* this is my function a */
}
a()
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
本文标签: jqueryWhy does the JavaScript need to start with quotquotStack Overflow
版权声明:本文标题:jquery - Why does the JavaScript need to start with ";"? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736783950a1952754.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论