This article mainly introduces the in-depth understanding of left query and right query in JavaScript. The editor thinks it is quite good. Now I will share it with you and give you a reference. Let’s follow the editor to take a look
Value and reference
Scope
In a narrow sense, a scope is an object (more specifically, it should be a collection);
In a broad sense, a scope is It is a set of rules for storing variables and making it easy to find these variables later;
The scope is responsible for collecting and maintaining the scope consisting of all declared identifiers A series of queries and enforces a very strict set of rules that determines the access rights of the currently executing code to these identifiers.
Preparation before execution of code (global code, function body, eval code):
1. Promotion (variable function function expression);
2. Determine the point of this;
3. Associated with the corresponding scope;
If the code segment is a function body, then on this basis, additional: parameter assignment, arguments assignment;
The relationship between scope and execution context:
A scope may contain several contexts. It is possible that there has never been a context (the function has never been called); it is possible that there has been, and now after the function is called, the context is destroyed;
Left query:
On the left side of the assignment symbol, who is the target of the assignment operation;
The relationship between the actual and formal parameters when the function is called is a left query;
The query of the variable on the left side of the equal sign. In the entire scope chain, if no declaration of a variable is found, the js engine will automatically declare a variable with the same name globally; however, the declaration of this variable will not be promoted.
(function(){ function test(a){ b=a; console.log(b);//2 } test(2); })(); console.log(b);//2
Right query:
The non-left side of the assignment symbol, who is the source of the assignment operation ;
Query for variables other than the left side of the equal sign. In the entire scope chain, if no declaration of a variable is found, a ReferenceError error will be thrown directly;
console.log(a);//ReferenceError: a is not defined
Special right query
//a并未定义赋值 console.log(typeof a);//undefined
Strict mode & non-strict mode
if(DEBUG){ //在生产环境中会报错 console.log("开始调试"); } if(typeof DEBUG !== "undefined"){ console.log("开始调试"); }
if(typeof polyfill_a === "undefined"){ //注意这一块不需要var,跟变量的提升有关 //这一块需要使用函数表达式而不是函数声明 polyfill_a = function(){ //功能代码 } }
Related recommendations:
The difference between left query and right query
The above is the detailed content of In-depth understanding of left query and right query in javascript. For more information, please follow other related articles on the PHP Chinese website!