Scope and closure

Compilation process: lexical analysis syntax analysis code generation

The engine is responsible for compiling and executing the entire JavaScript program from start to finish.

One of the compiler engine’s best friends, doing the dirty work of parsing and code generation (see the previous section).

Another good friend of the scope engine is responsible for collecting and maintaining a series of queries made up of all declared identifiers (variables), and enforcing a very strict set of rules that determine access to these identifiers by currently executing code.

The meaning of LHS and ** RHS** for “left or right of assignment operation” does not necessarily mean “= left or right of assignment operator”. Assignment operations take several other forms, so conceptually they are best understood as “who is the target of an assignment operation (LHS)” and “who is the source of the assignment operation (RHS)”.

RHS is the retrieve his source value

LHS is the retrieve his source value

Cheating on lexical

eval

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 when the book was written. In other words, you can programmatically generate code in the code you write and run it as if it were written in that location.

with

With is often used as a shortcut to refer repeatedly to multiple properties in the same object without having to refer repeatedly to the object itself.

b: 2, c: 3 }; Obj. A = 2; obj.b = 3; obj.c = 4; // Simple shortcut with (obj) {a = 3; b = 4; c = 5; }Copy the code

This and the prototype object

The binding rule for this

New is implicit by default

The binding priority of this

The ignored this

Object.create(null) is similar to {}, but does not create the object.prototype delegate, so it is “empty” than {} :

function foo(a,b) {
console.log( "a:" + a + ", b:" + b );
}
// Our DMZ empty object
var ø = Object.create( null ); // Expand the array into parametersFoo. Apply (ø, [2.3]);// a:2, b:3
/ / use the bind (..) Let's do that
varThe bar = foo. Bind (ø,2 );
bar( 3 ); // a:2, b:3


Copy the code