简体   繁体   English

如何正确阅读EcmaScript规范

[英]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

  1. Let lref be the result of evaluating LogicalANDExpression . lref成为评估LogicalANDExpression的结果。
  2. Let lval be ? lval成为 GetValue ( lref ). GetValuelref )。
  3. Let lbool be ToBoolean ( lval ). lboolToBooleanlval )。
  4. If lbool is false, return lval . 如果lbool为false,则返回 lval
  5. Let rref be the result of evaluating BitwiseORExpression . rref是评估BitwiseORExpression的结果。
  6. Return ? 回来? GetValue ( rref ). GetValuerref )。

And i read this like: 我读到这个就像:

  1. Take memory for left operand and here will be operation result 记住左操作数,这是操作结果

  2. Get value of left operand in memory and convert it to boolean 获取内存中左操作数的值并将其转换为布尔值

  3. if this boolean is false, return left operand 如果此布尔值为false,则返回左操作数

  4. 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,究竟什么是lrefrref ,那个上下文中的LogicalANDExpressionBitwiseORExpression究竟是什么?

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的精度):

  1. create reference lref to store the result of the evaluation of the left operand expression LogicalANDExpression 创建引用lref以存储左操作数表达式LogicalANDExpression的计算结果
  2. create lval that gets the value of lref (evaluates it and throws an error if there is some) 创建获取lref值的lval (计算它并在有一些时抛出错误)
  3. create lbool that gets the boolean value corresponding to lval 创建lbool ,获取与lval对应的布尔值
  4. if lbool is false, the whole operation will have the value lval and we stop here (that explains why 0 && 'foo' gets 0 as result, and not false ) 如果lbool为false,则整个操作将具有值lval并且我们在此停止(这解释了为什么0 && 'foo'得到0作为结果,而不是false
  5. (if not stopped at precedent step) create reference rref to store the result of the evaluation of the right operand expression BitwiseORExpression (如果没有在先前步骤停止)创建引用rref来存储右操作数表达式的评估结果BitwiseORExpression
  6. the whole operation will have the value of rref (still throws error if there is some. That explains why 1 && 'foo' gets 'foo' as result, and not 1 or true ) 整个操作将具有rref的值(如果有一些,仍然会抛出错误。这解释了为什么1 && 'foo'得到'foo'作为结果,而不是1true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM