While playing on the JS console, I came across a curious syntax. I wonder if anyone can tell me more about this.
Try the following:
>( 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 var token
Thus, expressions separated by commas inside brackets are evaluated, assignments are against the global scope, but links to function declarations remain trapped inside, like closing More ... placing this line inside a function declaration with a new one:
function C(){ ( function f(){console.log('i am f')} , (function x(){console.log('i am x')})() , y=2 , console.log('hello') ) }
and then the instance:
>var c=new C() i am x hello undefined >cy undefined >this.y 2
It happens exactly the same as on a global scale!
What is the use / purpose of this design?
Another:
>( function f(){console.log('i am f')} , f() ) ReferenceError: f is not defined
Thus, in the named function, you cannot refer inside the brackets.
source share