最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

scope - Javascript: rounded parentheses surrounding comma separated expressions - Stack Overflow

programmeradmin4浏览0评论

Playing on JS console I faced a curious syntax. I wonder if someone can tell me more on that..

Try this:

>( function f(){console.log('i am f')} , (function x(){console.log('i am x')})() , y=2 , console.log('hello') )
i am x
hello
undefined
>f()
ReferenceError: f is not defined
>this.y
2

This will fail:

( var c=2 ) SyntaxError: Unexpected token var

So ma separated expressions inside parentheses are evaluated, assignments happens to be against global scope, but named function declarations references stay trapped inside just like a closure more... putting that line inside a function declaration called with new:

function C(){
    ( function f(){console.log('i am f')} , (function x(){console.log('i am x')})() , y=2 , console.log('hello') )
}

and then instantiating:

>var c=new C()
i am x
hello
undefined
>c.y
undefined
>this.y
2

Happens exactly the same, just as executed in the global scope!

What's the usage / purpose of this construct?

One more:

>( function f(){console.log('i am f')} , f() )
ReferenceError: f is not defined

So the named function can't be referenced neither inside brackets.

Playing on JS console I faced a curious syntax. I wonder if someone can tell me more on that..

Try this:

>( function f(){console.log('i am f')} , (function x(){console.log('i am x')})() , y=2 , console.log('hello') )
i am x
hello
undefined
>f()
ReferenceError: f is not defined
>this.y
2

This will fail:

( var c=2 ) SyntaxError: Unexpected token var

So ma separated expressions inside parentheses are evaluated, assignments happens to be against global scope, but named function declarations references stay trapped inside just like a closure more... putting that line inside a function declaration called with new:

function C(){
    ( function f(){console.log('i am f')} , (function x(){console.log('i am x')})() , y=2 , console.log('hello') )
}

and then instantiating:

>var c=new C()
i am x
hello
undefined
>c.y
undefined
>this.y
2

Happens exactly the same, just as executed in the global scope!

What's the usage / purpose of this construct?

One more:

>( function f(){console.log('i am f')} , f() )
ReferenceError: f is not defined

So the named function can't be referenced neither inside brackets.

Share Improve this question edited Jul 20, 2015 at 16:47 VC1 1,6864 gold badges25 silver badges43 bronze badges asked Aug 18, 2013 at 21:46 aleclofabbroaleclofabbro 1,6991 gold badge19 silver badges37 bronze badges 1
  • The named functions aren't "trapped inside", because they're not function declarations (using function as a statement), they are in fact function expressions (using function as an operator). – Paul S. Commented Aug 18, 2013 at 21:49
Add a ment  | 

2 Answers 2

Reset to default 6

The named functions aren't "trapped inside", because they're not function declarations (using function as a statement), they are in fact function expressions (using function as an operator). This means that their names do not bee references to themselves in the current namespace.


Certain keyword/tokens can only be used as statements, such as var, hence an error is thrown if you try to use them in conditions which the interpreter expects to be an expression.


As for the y === 2, this is because you did not var y; inside C, so y = 2 sets window.y, and in the global scope this === window.


whats the usage / purpose of this construct?

  • The ma operator , lets you do multiple expressions on one line.
  • function expressions are useful for a great many things, be it immediately invoking them so you have a closure, or storing them inside variables, etc.

Wrapping code in parentheses forces it to be parsed as an expression.

The var keyword is only valid as a statement, so this creates a syntax error.

Function declarations can either be statements (which create a hoisted variable) or expressions (which do not create any variable).

Thus, wrapping the function in parentheses turns it into a function expression, which does not create an externally-visible name.

For more information, see here.

发布评论

评论列表(0)

  1. 暂无评论