les*_*ang 5 javascript expression referenceerror ecmascript-5
在JavaScript中,如果在赋值表达式的左侧放置某种表达式,引擎将抛出ReferenceError.例如,
// 'this' on the left side
this = window; // ReferenceError: Invalid left-hand side in assignment
Run Code Online (Sandbox Code Playgroud)
要么
// function call expression on the left side
var a;
var fn = function() {return a};
a === fn(); // true
a = 1; // 1
fn() = 5; // ReferenceError: Invalid left-hand side in assignment
Run Code Online (Sandbox Code Playgroud)
要么
var a;
a = 1; // 1
(a) = 2; // 2
(1, a) = 3; // ReferenceError: Invalid left-hand side in assignment
Run Code Online (Sandbox Code Playgroud)
我的问题是:
JavaScript也有l值和r值的概念为C吗?
为什么函数调用表达式不能出现在赋值表达式的左侧?在上面的例子中,因为和a === fn()之间有什么区别.我知道ES5规范要求这样做,但为什么它的设计是这样的呢?a = 5fn() = 5
根据赋值规范,以下表达式作为赋值目标无效:
this
Literal
ArrayLiteral
ObjectLiteral
FunctionExpression
ClassExpression
GeneratorExpression
RegularExpressionLiteral
TemplateLiteral
Run Code Online (Sandbox Code Playgroud)
例如,以下赋值也是无效的:
this = "bar"; // this
"foo" = "bar"; // Literal
/foo/ = "bar"; // RegularExpressionLiteral
["foo"] = "bar"; // ArrayLiteral
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2160 次 |
| 最近记录: |