Understanding Javascript_01 _ understanding the principle of memory allocation _ javascript skills

Source: Internet
Author: User
Before the official start, I would like to explain the javascript principles by analyzing the memory allocation during javascript execution, closure principle, face object, execution model, object model ..., the article has a special and in-depth perspective. I hope you can accept this form and provide valuable comments. Original Value and reference value
In ECMAScript, variables can be stored in two types of values: Original Value and reference value.
The original value indicates the value of the original data type (basic data type), that is, the value of the Undefined, Null, Number, String, and Boolean types.
Reference Value refers to the value of the composite data type, that is, Object, Function, Array, and custom Object.

Stack and stack
There are two types of memory corresponding to the original value and reference value: Stack and heap.
Stack is a type of data structure that is later-in-first-out. In javascript, Array can be used to simulate stack behavior.

The Code is as follows:


Var arr = []; // create a stack
Arr. push ("apple"); // The push element "apple" ["apple"]
Arr. push ("orange"); // The force-in element "orange" ["apple", "orange"]
Arr. pop (); // pop up "orange" ["apple"]
Arr. push ("banana"); // The push element "banana" ["apple", "banana"]


Let's take a look at the corresponding memory diagram:

Raw values are simple data segments stored in the stack, that is, their values are directly stored in the variable access location.
Heap is a data structure that stores data based on the hash algorithm. In javascript, reference values are stored in the heap.
The reference value is an object stored in the heap, that is, the value stored in the variable (that is, the variable pointing to the object, stored in the stack) is a pointer, point to the actual object stored in the heap.
For example, var obj = new Object (); obj is stored in the stack and points to the Object new Object (), while new Object () is stored in the heap.
So why should the reference values be placed in the heap, and the original values be placed in the stack, not in the memory? Why not put them together? Next, let's explore the answer to the question!
First, let's look at the Code:

The Code is as follows:


Function Person (id, name, age ){
This. id = id;
This. name = name;
This. age = age;
}
Var num = 10;
Var bol = true;
Var str = "abc ";
Var obj = new Object ();
Var arr = ['A', 'B', 'C'];
Var person = new Person (100, "jxl", 22 );


Then let's take a look at the memory analysis diagram:

The num, bol, and str variables are the basic data types. Their values are directly stored in the stack. obj, person, and arr are the composite data types, and their reference variables are stored in the stack, point to the actual object stored in the heap.

As we can see, we cannot directly manipulate the data in the heap, that is to say, we cannot directly manipulate the object, but we can operate the object through referencing the object in the stack, just as we operate the TV through a remote control machine, the difference is that the TV itself has no control buttons.


Now let's answer why the reference value should be placed in the heap and the original value should be placed in the stack:

Remember one sentence: energy is constant. It is nothing more than time-for-space or space-for-time.

The heap is larger than the stack, and the stack is faster than the heap operation speed. The object is a complex structure and can be expanded freely. For example, the array can be expanded infinitely, and the object can be added freely. Put them in the heap to avoid affecting the efficiency of the stack. Instead, find the actual object in the heap by referencing it and then perform the operation. Compared with simple data types, simple data types are relatively stable and occupy only a small amount of memory. Not putting simple data types in the heap is because it takes time to reference the actual object in the heap, and the overall cost is much greater than the cost of directly obtaining the actual value from the stack. Therefore, values of the simple data type are directly stored in the stack.

Summary:

The program is very simple, but it is the root of everything, and the foundation is the most important, because the building is also built with bricks and tiles.
Memory is the root of program execution. If you understand the memory, you can understand everything.
Encourage yourself and cheer up!

Refer:
JavaScript Advanced Programming
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.