什么是JavaScript语法中有效的左侧表达式? [英] What's a valid left-hand-side expression in JavaScript grammar?
问题描述
好的,我们都知道有效的左侧表达式是什么。种类。*
Okay, we all know what the valid left-hand-side expressions are. Kind of.*
但是,从 ECMA-Script标准,我很困惑:
LeftHandSideExpression :
NewExpression
CallExpression
这只是定义中的错误,还是我得到了这里有什么问题?我的意思是,这实际上并不意味着
Is that just an error in the definition, or am I getting something wrong here? I mean, doesn't that actually mean that
new Object = 1; // NewExpression AssignmentOperator PrimaryExpression
function () { return foo; }() = 1;// CallExpression AssignmentOperator PrimaryExpression
应该是有效的赋值表达式吗?
are supposed to be valid assignment expressions?
*从我的谦逊理解,这将更有意义:
* From my humble understanding, this would make much more sense:
LeftHandSideExpression :
Identifier
MemberExpression [ Expression ]
MemberExpression . IdentifierName
CallExpression [ Expression ]
CallExpression . IdentifierName
推荐答案
为了简明扼要地回答您的问题, LeftHandSideExpression
生产下的所有内容都是有效的 LeftHandSideExpression
。
To concisely answer your question, everything beneath the LeftHandSideExpression
production is a valid LeftHandSideExpression
.
我认为你真正问的问题是:
I think the question you are really asking is:
什么是有效的 LeftHandSideExpression
还可以分配?
What is a valid
LeftHandSideExpression
and also assignable?
答案是任何解析为 Reference
的内容,这是规范中明确定义的概念。在您的示例中
The answer to that is anything that resolves to a Reference
which is a well defined concept in the specification. In your example
new Object = 1;
新对象
是有效的 LeftHandSideExpression
但它没有解析为参考
。
The new Object
is a valid LeftHandSideExpression
but it does not resolve to a Reference
.
(new Object).x = 1;
左边是 MemberExpression。 IdentifierName
根据规范的最后一步是:
The left hand side is a MemberExpression . IdentifierName
which according to the spec the final step is:
返回类型为Reference 的值。 ..
如果你认为它是2个单独的属性更有意义。
If you consider it 2 separate properties it makes a lot more sense.
- 这是一个有效的LeftHandSideExpression吗?
- 这是一个有效的参考吗?
属性1在句法分析阶段确定,属性2在语义分析阶段确定。查看 8.7.2 PutValue(V,W)了解更多详情。
Property 1 is determined in the syntactical analysis phase and property 2 is determined in the semantic analysis phase. Check out 8.7.2 PutValue (V, W) for more details.
以下是规范本身的完整说明:
Here is a full explanation in the specification itself:
8.7参考规格类型
参考类型用于解释此类行为运算符为delete,typeof和赋值运算符。例如,期望赋值的左侧操作数生成引用。相反,赋值行为可以完全根据赋值运算符的左侧操作数的句法形式的案例分析来解释,但是对于一个难点:允许函数调用返回引用。纯粹为了宿主对象而承认这种可能性。此规范未定义的内置ECMAScript函数返回引用,并且没有为用户定义的函数提供返回引用的规定。 (不使用语法案例分析的另一个原因是它会冗长而笨拙,影响规范的许多部分。)
The Reference type is used to explain the behaviour of such operators as delete, typeof, and the assignment operators. For example, the left-hand operand of an assignment is expected to produce a reference. The behaviour of assignment could, instead, be explained entirely in terms of a case analysis on the syntactic form of the left-hand operand of an assignment operator, but for one difficulty: function calls are permitted to return references. This possibility is admitted purely for the sake of host objects. No built-in ECMAScript function defined by this specification returns a reference and there is no provision for a user-defined function to return a reference. (Another reason not to use a syntactic case analysis is that it would be lengthy and awkward, affecting many parts of the specification.)
在看了你的建议后,我相信它会抛弃某些有效的表达(注意:我不会宽恕这一点。)
After taking a look at your suggestion I believe it would throw off certain valid expressions (Note: I don't condone this.)
function OuterObj() {
this.Name = "Outer";
this.InnerObj = function() {
this.Name = "Inner";
}
}
var obj; (obj = new new OuterObj().InnerObj).Name = "Assigned";
这是 NewExpression
很重要的情况
这篇关于什么是JavaScript语法中有效的左侧表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!