Why can't the increment increment operator (++) be used for IIFE?

I am learning javascript and am amazed at the syntax IIFE

.

I realized that to write, IIFE

we need to make the function as an expression and then call it.

We can do this as an expression by wrapping a function between ()

. Or the prefix of the key functions of speech by using +

, -

, ~

, !

.

Now for the problem, when I prefix with ++

, I get an error in the console.

Code:

++function(){console.log("hello")}();

      

Mistake:

Uncaught ReferenceError: Invalid left-hand side expression in prefix operation

      

Why can't I use ++

? ++

is a unary operator, and I thought it would make the interpreter think of an anonymous function as a function expression, like +

, -

etc., did.

Where am I going wrong?

+3


source to share


2 answers


Unable to assign

As the error message says, function () ... is not a valid left expression, i.e. cannot be assigned.



+, -, ~ ,! will cause the expression to be evaluated. On the other hand, the increment (++) operator will cause the expression to not only be evaluated but also modified, which is not allowed for this expression.

+5


source


++

changes the value of the right side in place, so you have to put something in the RHS that can store any new value. The return value of the function call is passed to the left, but is not a storage location.



+3


source







All Articles