JavaScript Bang"!"功能与领先分叉“;” IIFEs [英] JavaScript Bang "!" Functions vs Leading Semi-Colon ";" IIFEs

查看:80
本文介绍了JavaScript Bang"!"功能与领先分叉“;” IIFEs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

气袋建议我这样做:

 !function(){
// ...
}();

因为:


这可以确保如果格式错误的模块忘记包含最终的
分号,那么当脚本获得
连接时,不会出现错误。


让我可以解决语言的语法规则:

  // //评估铬34控制台。 
function(){}(); // => SyntaxError:意外的标记(
!function(){}(); // => true



 !function(){}();函数(){}(); // => SyntaxError:意外的标记(
!function(){}();! function(){}(); // => true
function(){}());!function(){}(); // => true



然而,它看起来实际上并不安全,因为如果其他人在其脚本末尾没有分号:



<$ p (){}函数(){}()!函数(){}(); // => SyntaxError:意外的标记!
(function(){}())! function(){}(); // => SyntaxError:意外的令牌!


$ b

 ;(function(){
// ... $ (){}()); // =>未定义
(function(){b $ b $());

!function(){} }());(function(){}()); // => undefined
!function(){}();;(function(){}()); // => undefined
(function(){}());;(function(){}()); // => undefined

我错过了什么吗?是否真的可以接受使用砰!!功能或是领先的分号; IIFEs真正的优势在于它们连接的方式吗?

解决方案

你总是有IEFEs。无论是用括号包装它们,还是用作为前缀,都是您的选择,并没有什么不同。您需要任何一个强制将函数解析为表达式。请参阅 JavaScript函数领导砰!



无论您是使用; 来前缀整个结构以防止错误发生并置写得不好的脚本( JavaScript库中的主要分号是什么?)与完全无关。您可以根据需要混合使用模式:

 !function(){...}()//对于任意级联而言不安全$ 
;!function(){...}()
;(function(){...}())对于任意级联,b $ b(function(){...}

然而,有一种串联的情况,其中() vs 确实有区别:如果两个脚本连接在一起,以便在中间有一个换行符,而前者不以分号结尾。这确实允许自动分号插入跳入 - 当下一行确实以爆炸开始时!

  1 + 2 //脚本A 
!function(){...}(); //脚本B
//可以工作!

1 + 2 //脚本A
(function(){...}()); //脚本B
//运行时错误:2不是函数(或任何前一行结束)

我们了解到:始终以分号结束脚本。使用智能级联。如果您需要避免哑连接,请使用分号启动脚本。


Airbnd suggests I do this:

!function() {
  // ...
}();

Because:

This ensures that if a malformed module forgets to include a final semicolon there aren't errors in production when the scripts get concatenated.

The bang allows me to work around the language's grammar rules:

// Evaluated in Chromium 34 console.
function(){}(); // => SyntaxError: Unexpected token (
!function(){}(); // => true

And when concatenating other modules the bang seems to do the trick:

!function(){}();function(){}(); // => SyntaxError: Unexpected token (
!function(){}();!function(){}(); // => true
(function(){}());!function(){}(); // => true

However it doesn't seem to actually be "safe," because if someone else doesn't have a semi-colon at the end of his script:

!function(){}()!function(){}(); // => SyntaxError: Unexpected token !
(function(){}())!function(){}(); // => SyntaxError: Unexpected token !

It would appear that a leading semi-colon IIFE is better.

;(function() {
  // ...
}());

!function(){}();(function(){}()); // => undefined
(function(){}());(function(){}()); // => undefined
!function(){}();;(function(){}()); // => undefined
(function(){}());;(function(){}()); // => undefined

Am I missing something? Is it actually acceptable to use bang "!" functions or are leading semi-colon ";" IIFEs truly superior because of the way they concatenate?

解决方案

You always have IEFEs there. Whether you wrap them in parenthesis or prefix them with a ! is your choice, and doesn't make a difference. You need either one to force the function to be parsed as an expression. See javascript function leading bang ! syntax for details.

Whether you prefix that whole construct with a ; to prevent errors from concatenation with poor-written scripts (What does the leading semicolon in JavaScript libraries do?) is totally unrelated. You can mix the patterns as you want:

 !function(){…}() // not safe for arbitrary concatenation
 (function(){…}()) // not safe for arbitrary concatenation either
;!function(){…}()
;(function(){…}())

However, there is one case of concatenation where () vs ! does make a difference: if two scripts are concatenated so that there is a newline in between, and the former does not end in a semicolon. This does allow automatic semicolon insertion to jump in - when the next line does begin with a bang!

1 + 2             // script A
!function(){…}(); // script B
// works!

1 + 2              // script A
(function(){…}()); // script B
// runtime error: "2 is not a function" (or whatever the previous line ends in)

We learn: Always end your script with a semicolon. Use intelligent concatenation. Start your script with a semicolon if you need to be safe against dumb concatenation.

这篇关于JavaScript Bang&quot;!&quot;功能与领先分叉“;” IIFEs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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