There are things like
f.call(...)
f.apply(...)
But then there's this
(1, alert)('Zomg what is this????!!!11')
"1" does not seem to mean much in this context, the following works just fine:
(null, alert)('Zomg what is this????!!!11')
(1, null, alert)('Zomg what is this????!!!11')
(undefined, alert)('Zomg what is this????!!!11')
Could you point to a specific part of ECMAScript which describes that syntax?
You are just using The Comma Operator.
This operator only evaluates its operands from left to right, and returns the value from the second one, for example:
In the context of calling a function, the evaluation of the operand will simply get a value, not a reference, this causes that the
this
value inside the invoked function point to the global object (or it will beundefined
in the new ECMAScript 5 Strict Mode).For example:
As you can see, the first call, which is a direct call, the
this
value inside themethod
will properly refer toobj
(returning"obj.foo"
), the second call, the evaluation made by the comma operator will make thethis
value to point to the global object (yielding"global.foo"
).That pattern has been getting quite popular these days, to make indirect calls to
eval
, this can be useful under ES5 strict mode, to get a reference to the global object, for example, (imagine you are in a non-browser environment,window
is not available):In the above code, the inner anonymous function will execute within a strict mode code unit, that will result on having the
this
value asundefined
.The
||
operator will now take the second operand, theeval
call, which is an indirect call, and it will evaluate the code on the global lexical and variable environment.But personally, in this case, under strict mode I prefer using the
Function
constructor to get the global object:Functions that are created with the
Function
constructor are strict only if they start with a Use Strict Directive, they don't "inherit" the strictness of the current context as Function Declarations or Function Expressions do.