In following code
var x = { f: function () { return this === window } };
(0, x.f)();
I'm using construction (0, x.f)
to run function with this
equal to Window (or undefined in strict mode).
But typescript says
Left side of comma operator is unused and has no side effects.
But actually there is a side effect on this
of function I'm calling.
How should I write my code to eliminate this error message?
The comma operator ( , ) evaluates each of its operands (from left to right) and returns the value of the last operand. This lets you create a compound expression in which multiple expressions are evaluated, with the compound expression's final value being the value of the rightmost of its member expressions.
The comma operator in c comes with the lowest precedence in the C language. The comma operator is basically a binary operator that initially operates the first available operand, discards the obtained result from it, evaluates the operands present after this, and then returns the result/value accordingly.
A comma operator (,) in JavaScript is used in the same way as it is used in many programming languages like C, C++ etc. This operator mainly evaluates its operands from left to right sequentially and returns the value of the rightmost operand.
On the left-hand side of an assignment, the comma indicates that sequence unpacking should be performed according to the rules you quoted: a will be assigned the first element of the tuple, b the second.
Zero itself really doesn't have side effects, but comma operator does.
So the possible solution is to add as any
to 0
:
var x = { f: function () { return this === window } };
(0 as any, x.f)();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With