On the storage of JS data type

Source: Internet
Author: User

backgroundA classic question, first thrown to the folks to see:
var a = "Black Mao"; var b = a;var C = new Object (); var d = c;a = "New Black Mao"; c.age = 24;//print out what's the result? Console.log (A, B, C, D);
Above first put the problem out, after the carding, then look back to see the process. What are the data types of JS? JS packet content large data type, one is the basic data type (String, number, Boolean, undefined, null), and the other is a reference type (Object) What is the way data is stored?
    • Stack: The value of the parameter that holds the function, the values of the local variables (advanced post-exit)
    • Heap area: Distributed by programmers, distributed in a similar list (in any order)
    • Global zones: Global variables and static variables
    • Literal constant Area: constant
    • Program code area: function binary code
How are the underlying types and reference types stored in JS? The underlying type of data is stored in the stack, to give a simple example
var name = "Black Mao"; function Hello (user) {console.log (' hello ' + user);} Hello (name);//focus only on the storage of variables
Key Value
Name Black Mao
User Undefined

Data for reference types are stored in the heap area, for example
var a = new Object ();//Because the heap area is similar to a list, it is simple to use the list
Key Value
A Object A Address
Value of Object A: object{} ...

Another: The data stored in the heap is based on the address lookup value, and does not follow the queue or the stack's entry order Review

Look back at the beginning of the article, throw that question. You can reproduce the process from creation to assignment according to the storage method described above.
var a = "Black Mao"; var b = a;var C = new Object (); var d = c;
Storage structure of the stack area:
Key Value
A Black Mao
B Black Mao
C Object C Address
D Object C Address
Heap Area storage structure:
Value of Object C: object{} ... ...
A = "New Black Mao"; c.age = 24;
Storage structure of the stack area:
Key Value
A New Black Mao
B Black Mao
C Object C Address
D Object C Address
Heap Area storage structure:
Value of Object C: object{age:24} ... ...
Console.log (A, B, C, d);//New Black Mao black Mao Object{age:24} object{age:24}
SummaryClarify the process to make it more efficient and error-free when writing code. Organize at any time, also prevent oneself forget ~ reference: http://blog.jobbole.com/81010/http://blog.jobbole.com/81011/http://blog.jobbole.com/81018/

On the storage of JS data type

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.