我对javascript中的函数(对象)有疑问。 我有两段代码,比如
var a= function(){ console.log('abc') }和
var a= (function(){ console.log('abc') })这两者有什么区别>> 感谢名单:)
I have a doubt regarding functions(objects) in javascript. I have 2 pieces of code like,
var a= function(){ console.log('abc') }and
var a= (function(){ console.log('abc') })what is the difference between these two>> thanx:)
最满意答案
没有实际的区别。 它们都会导致将匿名函数分配给a 。
第一个是“ 简单的任务 ”。 在第二个中,括号充当“ 分组运算符 ”,它做了一件事:
生产PrimaryExpression:(Expression)的计算方法如下:
返回评估Expression的结果。 这可以是参考类型。
因此,分组运算符将返回其中包含的函数,并将其分配给a ,就像第一个示例一样。
There is no practical difference. They will both result in an anonymous function being assigned to a.
The first is a "simple assignment". In the second, the parentheses are acting as a "grouping operator", which does one thing:
The production PrimaryExpression : ( Expression ) is evaluated as follows:
Return the result of evaluating Expression. This may be of type Reference.
So the grouping operator will return the function contained within it, and assign it to a, just like the first example.
更多推荐
发布评论