Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the basic types and reference types in Javascript

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about what basic types and reference types are in Javascript. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. Basic type

a. There are 5 basic types, Number,String,Boolean,Null,Undefined

b. The underlying type has no concept of heap, and the heap is only for reference types.

All base types are stored in the stack as key-value, where the values of the base types are immutable unless they are re-assigned

For example:

Var a = "1234"; a [0] = "k"; / / modify the value itself, invalid console.log (a) / / 1234a = "k"; / / reassign console.log (a) / / k

two。 Reference type

Reference types such as Array,Object,Function,Date,RegExp, etc.

All reference types are stored on the stack in the form of key-value, where the value of the key-value of the reference type is the pointer address that points to a space in the heap where the value of the reference type data is stored.

Note:

The value of the reference data is "heap value", the heap value is value1, the key-value value inside the heap value object is the median value, and the heap value is "value2". In the current example, value1 is changeable and value2 is immutable unless it is re-assigned.

For example:

/ / heap value, that is, value1, can change var a = {"name": "jack"}; a = []; / / reassign the median value of console.log (a) / / [] / / heap value, namely value2, which is as immutable as the basic data type var a = {"name": "jack"}; a.name [0] = "0"; console.log (a) / / {"name": "jack"}

3. Transfer value and address

As shown in the following figure:

3.1 A code block is passed value

A code block executes, and both an and b open up a space in the stack area to store the value 10 and store it in key-value form. A code block is a value passed.

3.2 B code block is the address

B code block execution, A1 will open up a space to store the pointer address, and then A1's pointer address will be assigned to b1, that is, b1 will also open up a space to put the pointer address, and A1 pointer address will point to the "heap value" of the heap area, that is, value1.

4. Compare

4.1 the comparison of basic types is the comparison of values

For example:

Var a = 1 position var b = 1 port console.log (a = = b); / / true

4.2 comparison of reference types is a comparison of references

The comparison of the same reference can be equal, but the comparison of different references is different.

For example:

/ / comparison of different references var p1 = {}; var p2 = {}; console.log (p1 = = p2) / / false / / comparison of different references var p3 = {"name": {}, "age": 30}; var p4 = {"name": {}, "age": 30}; console.log (p3.name = = p4.name); / / false (this is a reference comparison) console.log (p3.age = = p4.age) / / true (this is a value comparison) / / compare with the quoted var p5 = {}; var p6 = p5ash console.log (P5 = = p6) / / true

If the reference is assigned to another type, the current reference is cut off and the pointer address of the value.

As shown below:

When A1 is reassigned, the connection between A1 and "heap value-- > {}" is replaced with a new pointer address, that is, "heap value-- > []"

Thank you for reading! This is the end of the article on "what are the basic types and reference types in Javascript". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report