为什么使用(function(){})()或!function(){}()? [英] Why use (function(){})() or !function(){}()?

查看:156
本文介绍了为什么使用(function(){})()或!function(){}()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在阅读在JavaScript中,优点!function(){}()over(function(){})()?然后它打我,为什么使用:

I was reading In JavaScript, what is the advantage of !function(){}() over (function () {})()? then it hit me, why use :

(function(){})()!function(){}() > function(){}()?

(function(){})() or !function(){}() instead of just function(){}()?

有什么特别的原因吗?

推荐答案

这取决于在哪里 function(){}()本身会产生语法错误,因为它被评价为函数声明和那些需要的名称。

It depends on where you write this. function(){}() by itself will generate a syntax error as it is evaluated as function declaration and those need names.

通过使用括号或非运算符,您可以强制将其解释为函数表达式,它们不需要名称。

By using parenthesis or the not operator, you enforce it to be interpreted as function expression, which don't need names.

如果它将被视为表达式,则可以省略括号或运算符。

In case where it would be treated as expression anyway, you can omit the parenthesis or the operator.

这篇关于为什么使用(function(){})()或!function(){}()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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