gpt4 book ai didi

javascript - 为什么 JavaScript 需要以 ";"开头?

转载 作者:IT老高 更新时间:2023-10-28 13:13:47 25 4
gpt4 key购买 nike

我最近注意到 Web 上的很多 JavaScript 文件都以 ; 开头,紧跟在评论部分之后。

例如,this jQuery plugin's代码以:

开头
/**
* 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( $ ){

为什么文件需要以 ; 开头?我在服务器端 JavaScript 文件中也看到了这种约定。

这样做有什么好处和坏处?

最佳答案

我会说,由于脚本经常被连接和缩小/压缩/发送在一起,最后一个人有可能有类似的东西:

return {
'var':'value'
}

在最后一个脚本的末尾没有 ; 在末尾。如果你有一个 ; 在你的开头,它是安全的,例如:

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

关于javascript - 为什么 JavaScript 需要以 ";"开头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2481543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com