admin管理员组文章数量:1401915
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 (usingfunction
as an operator). – Paul S. Commented Aug 18, 2013 at 21:49
2 Answers
Reset to default 6The 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.
本文标签: scopeJavascript rounded parentheses surrounding comma separated expressionsStack Overflow
版权声明:本文标题:scope - Javascript: rounded parentheses surrounding comma separated expressions - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744335293a2601160.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论