Search code examples
javascriptfunctionargumentsfunction-call

Are function calls with arguments valid left-Hand-Side-Expressions according to ECMAScript


Are function calls with arguments valid left-Hand-Side-Expressions according to ECMAScript ?

LeftHandSideExpression[Yield, Await] :
      NewExpression[?Yield, ?Await]
      CallExpression[?Yield, ?Await]
      OptionalExpression[?Yield, ?Await]

if we go further deep into CallExpression we can see below the non-terminal CallExpression something like this:

CallExpression[?Yield, ?Await] Arguments[?Yield, ?Await]

does that mean writing something like:

alert('Hello') = 1; 

is valid. And if you run the above code you will see that the function call takes place before the ReferenceError: Invalid left-hand side in assignment is given


Solution

  • It's a very good question, and LeftHandSideExpression may not be the ideal name for that production; it's used even when there may be no right-hand side. :-)

    Later in the spec, when deciding whether the target of an assignment is valid, the spec checks the AssignmentTargetType of the expression (here). If that type is not simple, the assignment is invalid. The AssignmentTargetType for CallExpression Arguments (e.g., alert('Hello')) and a few others (super(), etc.) is invalid (here). So you can't assign to alert('Hello').