[英]How to properly read EcmaScript specification
I'm interested in "HOW it work" knowledge for JavaScript binary logical operations, but I'm stuck at step interpritation. 我对JavaScript二进制逻辑操作的“如何工作”知识感兴趣,但我陷入了一步一步的错误。
So that's description of specs 这就是规格的描述
12.13.3 12.13.3
LogicalANDExpression: LogicalANDExpression && BitwiseORExpression LogicalANDExpression:LogicalANDExpression && BitwiseORExpression
- Let lref be the result of evaluating LogicalANDExpression . 让lref成为评估LogicalANDExpression的结果。
- Let lval be ? 让lval成为? GetValue ( lref ). GetValue ( lref )。
- Let lbool be ToBoolean ( lval ). 设lbool为ToBoolean ( lval )。
- If lbool is false, return lval . 如果lbool为false,则返回 lval 。
- Let rref be the result of evaluating BitwiseORExpression . 设rref是评估BitwiseORExpression的结果。
- Return ? 回来? GetValue ( rref ). GetValue ( rref )。
And i read this like: 我读到这个就像:
Take memory for left operand and here will be operation result 记住左操作数,这是操作结果
Get value of left operand in memory and convert it to boolean 获取内存中左操作数的值并将其转换为布尔值
if this boolean is false, return left operand 如果此布尔值为false,则返回左操作数
Else take memory for right operand and here will be... BitwiseORExpression <-- what? 否则为右操作数记忆,这里将是... BitwiseORExpression < - 什么? Bitwise? 按位? for what? 为了什么? why? 为什么?
I'd would like to clarify this algorithm in more human-readable form for understanding how it works. 我想以更易读的形式澄清这个算法,以便了解它的工作原理。 What is p.1 and p.5, what really is lref and rref , what really is LogicalANDExpression and BitwiseORExpression in that context? 什么是p.1和p.5,究竟什么是lref和rref ,那个上下文中的LogicalANDExpression和BitwiseORExpression究竟是什么?
BitwiseORExpression here just refers to the (expression for the) right operand, exactly like LogicalANDExpression refers to the left one as you correctly deduced. 这里的BitwiseORExpression只是引用右操作数的(表达式),就像你正确推导出的LogicalANDExpression指的是左边的那个。 What it is depends on the code (or rather its parsed form, the AST) that you are currently evaluating, it could be basically anything and does not need to contain an |
它取决于您当前正在评估的代码(或者它的解析形式,AST),它基本上可以是任何东西 ,并且不需要包含|
operator. 运营商。
This is how i read it (thanks to Bergi for the precisions): 这就是我读它的方式(感谢Bergi的精度):
0 && 'foo'
gets 0
as result, and not false
) 如果lbool为false,则整个操作将具有值lval并且我们在此停止(这解释了为什么0 && 'foo'
得到0
作为结果,而不是false
) 1 && 'foo'
gets 'foo'
as result, and not 1
or true
) 整个操作将具有rref的值(如果有一些,仍然会抛出错误。这解释了为什么1 && 'foo'
得到'foo'
作为结果,而不是1
或true
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.