JavaScript中经常遇到逗号操作符。逗号操作符语法:
Expression[In, Yield] :
AssignmentExpression[?In, ?Yield]
Expression[?In, ?Yield] , AssignmentExpression[?In, ?Yield]
可以看到在各种表达式中都可以使用。
在来看看运行时语义:
Expression : Expression , AssignmentExpression
1. Let lref be the result of evaluating Expression.
2. ReturnIfAbrupt(GetValue(lref))
3. Let rref be the result of evaluating AssignmentExpression.
4. Return GetValue(rref).
第一步是让lref的值为Expression表达式的值
第二步如果GetValue(lref)则返回
第三步是让rref的值为AssignmentExpression表达式的值
第四步是返回GetValue(rref)的值
可见逗号表达式返回的是最后一个表达式的值。