This is the 24th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
Ready to write some crap, some JavaScript bullshit
Compose a poem:
The object has no root,
Type is invisible.
There was nothing there,
Where does dust come from?
First, JavaScript memory model
The essence of JavaScript is an Object, and an Object can contain multiple attributes. The attributes of the Object can be divided into two types: direct quantity and Object, and Object is divided into Object and function Object.
By data type:
- Simple data types
- number
- string
- boolean
- Special data type
- null
- undefined
- Complex data types
- object
- Object
- The function object
- object
The two types of properties of immediate quantities and objects are held differently in memory (similar to Java)
-
Direct quantity: Directly use two pieces of memory to store the attribute name and value respectively
-
Object: Three blocks of memory are required to store the property name, property address, and property content
For properties of object types, property name knowledge refers to the memory address that holds the object, rather than executing the actual object, as shown in the following code:
function F(){
this.v = 1;
}
var f1 = new F();
var f2 = f1;
console.log(f2.v);
f2.v = 2;
console.log(f1.v);
f1 = null;
console.log(f2.v);
Copy the code
Code execution diagram:
We’ve always said that JavaScript is a scripting language that interprets execution in the browser and shouldn’t have its own memory model, but that’s not the case. In both compiled and interpreted languages, data such as variables, functions, and objects are stored in memory. When you use them, you need to find the specific content in the specified place by using the variable name, and then perform actual operations.
How are functions executed in JS
Functions, which we’ve seen before, have two parts: data and operations on data. Data can be divided into external data and internal data. For external data we will not explain, here is mainly said function, internal data is divided into parameters and variables two parts.
Parameter: the parameter is assigned a new value each time the function is executed;
Variables (local variables) : set to the same initial value each time;
-
How are the variables and arguments of a function stored?
The function creates an array of parameters and an array of variables before each execution (it can also be combined into an array, which is usually done using a stack), and then sets the parameters passed on the call to the array of parameters, which has the same contents each time. Simple data is stored directly in an array, whereas complex data, where the array just holds the address, is stored in the heap.
function paramTest(p1){ var message = "Hello World"; console.log(p1); for(var i in arguments) {console.log(arguments[i]); }}// Function call paramTest("a"."b"."c"); // The output is a, A, b, and c Copy the code
We use Chrome debugging, the function in the execution of the parameter P1 and the variables used in the function messag, I is the same status, that is, the internal execution of the function does not distinguish between parameters and variables. In a JS function, an internal variable named Arguments is automatically created and the addresses of all arguments are saved into it. Arguments is like an array object that can be used to get the arguments passed during a function call.
ParamTest (); / / paramTest (); / / paramTest (); / / paramTest (); / / paramTest (); When a function is called, the number of parameters can be different from the definition, so there is no overloaded method of the same function in JS.
-
Function – defined variables are scoped at the function level instead of the block level
function scopeTest(){ if(true) {var message = "Hello World"; } console.log(message); } // Function call scopeTest(); // The output is Hello World Copy the code
Here message is defined within the if block, but can still be called outside the if statement.
When a method in JS executes, it will put all its variables defined by var into the array of variables introduced above, so all variables defined by var are equal in a function