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 javaScript reference types and basic types

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

Share

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

This article mainly explains the "javaScript reference types and basic types", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "javaScript reference types and basic types" bar!

1. Concept:

The basic type is also called simple type, and the data stored is single, such as: the number of students is just a number; the reference type is also called complex type, and the stored data is complex, such as: students, including student number, name, gender, age and so on. From the perspective of memory (if you don't understand memory, please check the relevant information): the basic type occupies only one area of memory; the reference type occupies two areas of memory. That is, when defining variables of basic types, only one space is applied in memory, and the values of variables are stored directly in that space; when defining variables of reference types (it is easy to understand that we see the new operator, that is, variables that define reference types), we apply for two blocks of space in memory, the first space stores the address of the second space, and the second space stores real data. The first block of space is called the reference (address) of the second block of space, so it is called the reference type.

The basic types of javaScript include: Number, String, Boolean, and Null,Undefined.

The reference type of javascript is: Object. Array,Date belongs to the Obejct type.

two。 Memory diagram:

The following code (both define two local variables):

Function demoFun () {

Var num = 20 position / defines a basic type of variable.

Var arr = new Array (122.23,34); / / defines a variable of reference type

}

The memory diagram of the above two lines of code:

As you can see, the num variable occupies only one memory area; the arr variable occupies two memory areas, and the arr variable applies for a memory area in the stack area (for those who don't understand the stack area, don't think too much), storing the address, which is the address of the heap area. The data is really stored in the heap area, so the arr variable occupies two memory areas. In this way, variables of reference types also seem to take up a lot of memory. Haha, don't worry, after you understand the advantages of reference types, you will think this is a problem.

When we read the value of the num variable, we can read it directly, but when we want to read the value in arr, we first find the address in arr, and then find the corresponding data according to the address.

Reference type, similar to shortcuts in the windows operating system. A shortcut is an address, and the real content is the content of the path that the shortcut points to. For example, if we put the d:\ t.txt file to create a shortcut on the desktop, then the shortcut on the desktop will take up the space of the desktop, and d:\ t.txt will take up the space of the d disk, so it takes up two pieces of space.

The basic type is the equivalent of a file.

Reference type, similar to when we fill in the application form, fill in the home address, this home address is equivalent to the first piece of space, the real home (the second piece of memory space) is not on the application form. If the school wants to find your home, first find your address on the registration form, and then find your home according to the address.

3. Advantages of reference types:

When the reference type is used as a parameter of the function, the advantage is particularly obvious: first, when the parameter is passed to the argument, only the address needs to be passed, and there is no need to move a large amount of data (saving memory overhead); second, when the data corresponding to the parameter changes, the data corresponding to the parameter is also changing (in many cases, we hope so).

Such as the following code:

Define the function first (bubble sort)

Function bubble (arr) {

For (var iTuno Bandi)

For (var juni0witj

If (arr [j] > arr [juni1]) {

Var temp = arr [j]

Arr [j] = arr [juni1]

Arr [jacks 1] = temp

}

}

}

}

When bubble sort is called

Var arr1 = [250, 250, 2, 900, 35, 12, 99]

Bubble (arr1)

Look at the changes in memory when the code above memory is executed:

In the figure, when the code corresponding to ① is executed (var arr1 = [250jin2pc 290jr35];), the corresponding change of ① will occur in memory, that is, a memory area is applied in the stack, named arr1, and the memory space in the heap area is placed in 25029035051012j99, and the address of the memory in the heap area is assigned to the memory of arr1; when the corresponding code bubble (arr1) of ② is executed, the function is called. At this time, the formal parameter arr (the change corresponding to the ③ in memory) is defined, that is, a memory area is applied in the stack, named arr, and the address saved by the arr1 is assigned to the arr (the assignment represented by the ② in memory), so that the formal parameter arr and the argument arr1 point to the same memory area. There is only one copy of the value 250 in the array, 250, 2, 29, 35, 12, 99 in memory. That is, you don't have to make another copy of the value of each element in the array, saving memory. If you understand the memory graph, then when the data order corresponding to the parameter arr is changed, the data order corresponding to the parameter arr1 is also changed. That is, when the parameter data is changed, the actual parameter data is also changed. Therefore, the bubble function does not need to return a value, and can still achieve the purpose of sorting. You can run the code in my example to see if the sorting effect is achieved.

In addition, the memory variation of the basic type as a function parameter:

Memory diagram:

4. Assignment of reference type variable:

When a reference type variable is assigned, it is assigned an address. That is, the same block address is stored in the two reference type variables, that is, both reference type variables point to the same memory area. Therefore, the data corresponding to the two reference type variables is the same.

Another example is:

Var person1 = {

Name: Zhang San

Sex: "male"

Age:12

}

Var person2 = person1

Person2.name= "Zhang Si"; / / this sentence will change the name of person1 and person2. Indicates that the name of person1 and person2 occupy the same block of memory.

Alert (person1.name+ "," + person1.sex+ "," + person1.age)

Alert (person2.name+ "," + person2.sex+ "," + person2.age)

The memory change when a primitive type variable is assigned.

5. Shallow copy and deep copy

First of all, let's talk about the replication of the object. as mentioned above, the assignment of the reference type (object) is only the assigned address, so what should we do when we really copy a new object (that is, clone).

Var person1 = {

Name: Zhang San

Sex: "male"

Age:12

}

Var person2= {}

For (var key in person1) {

Person2 [key] = person1 [key]

}

However, when the property of an object is another reference type, there will be problems of shallow copy and deep copy. Use a custom object type to illustrate the problem.

Such as:

Var person1 = {

Name: Zhang San

Sex: "male"

Age:12

Address: {

Country: Shaanxi

City: Weinan

}

}

/ / the address of the object person1 is also an object, that is, to make a real clone of the person1, you need to clone each attribute in the address.

Var person2= {}

For (var key in person1) {

Person2 [key] = person1 [key]

}

Person2.name= "Zhang Si"; / / does not change the name attribute of person1.

Person2.address.country= "Beijing"; / / will change the address.country of person1

Notice that the name properties of person1 and person2 each have their own space, but person1.address.country and person2.address.country are the same space. Therefore, when you change the value of person2.address.country, the value of person1.address.country also changes. This means that the copy (clone) is not in place, this kind of copy is called shallow copy, and further copy (clone) person1.address.country and person1.address.name, that is, deep copy. To make a deep copy, you need to determine the type of each attribute, and if it is a reference type, cycle through the copy (recursion is required).

Thank you for your reading, the above is the content of "javaScript reference types and basic types". After the study of this article, I believe you have a deeper understanding of what javaScript reference types and basic types have, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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