1. The memory space in JS is divided into two types: stack memory and heap memory
Stack memory: Provides an environment for JS code execution; stores the values of basic data types; -> Global scope or private scope are actually stack memory
Heap memory: stores the value of the reference data type (the object stores the attribute name and attribute value in it, and the function stores the code in the function body as a string)
2. When the browser loads our HTML page, it will first provide an environment for JS code execution -> global scope (global->window)
3. Before the JS code is executed, the browser needs to do some things by itself: declare or define all the var/function keywords in advance -> "pre-explanation" (variable sound)
Declare -> Tell the browser that I have such a thing, such as var num1; function fn;
Defined -> Assign value to the declared variable or function, for example num1=12; fn=function(){}
[Important] Variables only declare that they are not defined, the default value is undefined (undefined)
4. The processing of var and function is different in the pre-interpretation stage.
var -> When pre-explanation is performed, the variable is declared in advance. The assignment operation will be completed only when the code is executed.
function -> During pre-explanation, all declarations and definitions will be completed in advance (when the code is executed, it will be skipped directly)
[Important] At the beginning, only pre-interpretations are performed on window. The current strings stored in the fn function are all strings, so var total has no practical meaning, so pre-interpretations are not performed -> "Pre-interpretations occur in the current scope"
console.log(obj);//->undefined var obj = {name: "Zhang Shanshan", age: 10}; function fn(num1, num2) {//Skip directly when the code is executed to this line, because during the pre-explanation we have completed the declaration and definition var total = num1 + num2; console.log(total); } var num1 = 12; fn(num1, 100);//Execute fn, assign the value of the global variable num1 to the parameter num1, and assign 100 to the parameter num25. The variable declared under the global scope is a global variable
Variables declared in private scope are private variables; formal parameters of functions are also private variables;
How to tell whether the variables appearing in a function are private or global?
First, see if it is a formal parameter, and then see if it has been declared in the private scope (has vared). One of the two is a private variable. Then no matter where it appears in the current function, it is private, and there is no relationship between it and the global one; if neither is it, it means it is not private, then search for the previous scope...
6. When the function is executed, a new private scope (stack memory) will be formed for the code in the function body to execute;
1) Assign value to formal parameters
2) Pre-interpretation under private scope
3) Code execution under private scope
The new private scope formed also protects the private variables inside from external influences. We put this protection mechanism of the function ->"closure
Difference: Those with var can be declared before the code is executed, while those without var cannot be declared in advance
1. Pre-explanation is required regardless of whether the conditions are true or not.
window pre-explanation:var a; -> window.a; if (!("a" in window)) {//"a" in window -> true var a = "us"; }console.log(a);//->undefined2. Pre-interpretation only occurs on the left side of "=", only the left side is pre-interpretation, and the right side is the value without pre-interpretation.
Function expression of anonymous functions: assign the part of the function definition as a value to an event that is a variable or element.
When pre-explanation: var fn; -> The default value of fn is undefined
fn();//->undefined() Uncaught TypeError: fn is not a function Only functions can be executed in JS&& If the above code above JS is reported, the code below is not executed without any special processing var fn = function () { console.log("ok"); }; fn(); When pre-explanation: fn=xxxfff000 fn();//->"ok" function fn() { console.log("ok"); } fn();//->"ok" function fn() { console.log("ok"); } fn();//->"ok"3. The code below the return in the function body is no longer executed, but the code below needs to participate in pre-explanation; and the things behind the return need to be processed, but since it is returned as a value, pre-explanation is not performed;
var total = 300; function fn() { console.log(total); return function sum() {};//return is to return the value in the function to the outside of the function, which returns the memory address corresponding to the function to the outside of the function, for example: return xxxfff111; the code below return in the function body is not executing var total = 10; } fn();4. The function of anonymous function is not pre-explained under the global scope.
The self-executing function of anonymous function: the definition and execution are completed together
(function(num){})(100);
5. When pre-explanation, if the name is repeated, it will only be declared once and not repeated, but the assignment will still be repeated.
If the name of a variable and the name of a function are the same as the name of a function in JS, it is considered to be repeated
Pre-explanation:
var fn; declare fn = xxxfff000; [Declaration] Don't + define fn = xxxfff111; [Declaration] Don't + definition ->fn=xxxfff111 var fn = 12;//window.fn=12 function fn() {//window.fn=function(){} } function fn() { }The above is all the relevant understandings about JS pre-explanation brought to you by the editor. I hope you will support Wulin.com more~