简体   繁体   English

JavaScript LHS 和 RHS 查找

[英]JavaScript LHS and RHS Lookup

I was reading the Scopes and Closure of You don't know JS book by Kyle Simpson, specifically this topic Compiler Speak .我正在阅读 Kyle Simpson 所著的 Scopes and Closure of You don't know JS 一书,特别是这个话题Compiler Speak

There they mention the LHS and RHS lookup .在那里他们提到了LHS 和 RHS 查找 I am failed to understand these two terms, can anyone help me to realize them?我无法理解这两个术语,谁能帮我实现它们?

LHS look-up is done when a variable appears on the left-hand side of an assignment operation, and an RHS look-up is done when a variable appears on the right-hand side of an assignment operation.当变量出现在赋值操作的左侧时进行 LHS 查找,当变量出现在赋值操作的右侧时进行 RHS 查找。

I think of it as follows :我认为它如下:
lhs lookup is a container lookup lhs 查找是一个容器查找
rhs lookup is a value lookup rhs 查找是一个值查找

Don't think of it as a left hand side or right hand side assignment, think of it as storing the value into memory and retrieving it later.不要将其视为左侧或右侧的赋值,而是将其视为将值存储到内存中并稍后检索。

For example when you type b in the chrome developer console, it starts the RHS lookup (retrieve the value of b ) and if the value is not found, it throws ReferenceError .例如,当您在 chrome 开发人员控制台中键入b时,它会启动RHS查找(检索b的值),如果未找到该值,则会抛出ReferenceError

In contrast when you type the b = 2 in chrome developer console, it starts LHS lookup, and if b is not found in the nested scope the JS compiler declares it in the global scope (depending on whether you are running code in strict mode or not).相反,当您在 chrome 开发者控制台中键入b = 2时,它会启动LHS查找,如果在嵌套作用域中找不到 b,则 JS 编译器会在全局作用域中声明它(取决于您是在strict mode还是在strict mode运行代码)不是)。

For example take following code into consideration例如考虑以下代码

 function foo(a) { console.log( a + b) } foo( 2 );

When the JS compiler executes the code it first looks for the function foo and whether it is declared in the current scope (Hosting Environment Here) which is called RHS lookup.当 JS 编译器执行代码时,它首先查找函数foo以及它是否在当前范围(此处为托管环境)中声明,这称为 RHS 查找。 Now in the scope of foo the argument will be 2 and as we declared function foo(a) when we wrote foo( 2 ) we are implicitly assigning the value 2 to a or a = 2 , which is called LHS lookup (assigning value 2 to a ), now fast-forward the compiler will come to line console.log( a + b) again it will look for value a and b (again RHS lookup) and if the values are found it will assign it to console.log argument (if you assume that console.log is defined as console.log(arg1) in the hosting environment then arg1 = value of a+b (which is again LHS lookup).现在在 foo 的范围内,参数将是 2 并且当我们在编写foo( 2 )时声明函数foo(a) foo( 2 )我们隐式地将值2分配给aa = 2 ,这称为LHS查找(分配值2a ),现在快进编译器将再次来到console.log( a + b) ,它将查找值ab (再次RHS查找),如果找到值,则将其分配给console.log参数(如果您假设在托管环境中console.log被定义为console.log(arg1)arg1 = value of a+b (这也是LHS查找)。

In short:简而言之:

• when the engine retrieves the value of a variable console.log(b) , it gets the value from the memory location of b . • 当引擎检索变量console.log(b)的值时,它从b的内存位置获取值。 This is a RHS lookup这是一个RHS查找

• when the engine assigns the value to the variable b = 2 , it looks for the value of b in the scope – if found, it sets the value 2 in the memory location of b , if not, it look in the upper level scope. • 当引擎将值赋给变量b = 2 ,它在作用域中查找b的值——如果找到,则在b的内存位置中设置值2 ,如果没有,则在上层作用域中查找. This is a LHS lookup.这是LHS查找。

Simple example from the same book you have mentioned您提到的同一本书中的简单示例

function foo(a) {
console.log( a ); // 2
}
foo( 2 );

LHS : When you pass a value(2) to the foo method , compiler will assign to the parameter as a = 2 , is called LHS Lookup. LHS :当您将 value(2) 传递给 foo 方法时,编译器会将参数分配为a = 2 ,称为 LHS 查找。 It will simply find a container variable to assign the value.它会简单地找到一个容器变量来分配值。

RHS :In order to execute console.log to print a , need an RHS reference for the value of a. RHS :为了执行console.log 打印a ,需要a 值的RHS 引用。 It's called RHS Lookup它被称为 RHS 查找

Another Example另一个例子

function foo(a) {
var b = a;
return a + b;
}
var c = foo( 2 );

3 LHS from the above example -**上例中的 3 LHS -**

  1. c = (container to hold the return of foo method) c = (保存 foo 方法返回值的容器)
  2. a = 2 (When you pass value 2 to the method, compiler will assign a = 2) a = 2 (当你将值 2 传递给方法时,编译器会分配 a = 2)
  3. b =乙 =

4 RHS from the above code snippet 4 来自上述代码片段的 RHS

  1. foo(2) - need a reference of a to get the value foo(2) - 需要 a 的引用来获取值
  2. = a - to get the value of b , need a reference of a = a - 要获取 b 的值,需要 a 的引用
  3. a - to get the value of a , need a reference of a a - 要获取 a 的值,需要 a 的引用
  4. b - to get the value of b , need a reference of b b - 要获取b的值,需要 b 的引用

Edit:编辑:

LHS - Look for an identifier for assigning purposes or for assigning a value to it. LHS - 查找标识符以用于分配目的或为其分配值。

let foo;

// It's looking for foo identifier in global scope and assign value
// So it's an LHS 
foo = 1; 

// It's also LHS as it assigning new value
foo = 2;

Now, RHS - it means when you're looking for an identifier to use it (not to assign value)现在,RHS - 这意味着当你正在寻找一个标识符来使用它(而不是分配值)

function foo() {
    alert(2);
}

// this look for an identifier called foo and 
// in global scope it's a function decoration 
// and execute it
foo();

I think of it as follows : lhs lookup is a container lookup rhs lookup is a value lookup我认为它如下: lhs 查找是一个容器查找 rhs 查找是一个值查找

I like Kyle Simpson's approach a lot but this particular nutshell explanation brought home the point loud and clear to me.我非常喜欢凯尔辛普森的方法,但这个特别的简明解释让我明白了这一点。

There's always a trade between "just tell what I need to know, nothing more" and drilling down to understand better at a deep level. “只说我需要知道的,仅此而已”和深入了解以在更深层次上更好地理解之间总是需要权衡取舍。

Sometimes that deep understanding can help you keep out of trouble, how to debug, write test code, optimize and refactor.有时,这种深刻的理解可以帮助您摆脱麻烦,如何调试、编写测试代码、优化和重构。

I am currently reading and watching a lot of Kyle's writing and online teaching, he does have knack for explaining things well.我目前正在阅读和观看凯尔的大量写作和在线教学,他确实有很好的解释能力。 A lot of instructors lose people along the way either by going too fast because the have expertise and it's hard for them to slow down - on the other hand if you get too basic the talk gets uninteresting and you just tune out.很多讲师在此过程中会失去一些人,要么因为他们有专业知识而走得太快,而且他们很难放慢脚步——另一方面,如果你太基础了,谈话就会变得无趣,你就会退出。

Or short:或简短:

RHS - for read lookup RHS - 用于读取查找

LHS - for write lookup LHS - 用于写查找

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

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