LHS and RHS (LVALUE and Rvalue)

LHS: Left side of the assignment operation.

RHS: Finds a value.

function foo(a) {
  var b = a
	return a + b
}
foo(2)
Copy the code

Q: How many LHS and RHS queries does the above code have?

  • Foo (2) find the foo ()[RHS] , a = 2 [LHS]
  • Var b = a assigns b[LHS], looking for a[RHS]
  • Return a + b to find a[RHS]A and b[RHS]

When RHS query fails, ReferenceError is reported.

Lexical scope

Lexical scope vs dynamic scope

Lexical scope is the scope of the definition at the lexical stage.

Javascript uses lexical scope, and the scope of a function is determined at function definition,

Dynamic scoping, on the other hand, is a function whose scope is determined at the call stage.

var b = 1
function foo(){
	var b = 2
  	bar()
}

function bar() {
	console.log(b)
}

foo()
Copy the code

If there is no local variable b inside the bar function, it will look for the code in the previous layer according to the position of writing, that is, b = 1, so it will print 1.

In the Javascript eval (…). A function can take a string as an argument and treat its contents as if they existed at that point in the program at the time of writing.

That is, you can programmatically generate code in the code you write and run it as if it were written in that location.

Instead of eval, it dynamically modifs the lexical scope, causing the JS engine to find it in the code at compile time and unable to optimize it, resulting in poor code performance.