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 is the difference between JavaScript basic data types and reference types

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail the difference between JavaScript basic data types and citation types. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Js basic data types:

Js basic data types include: undefined,null,number,boolean,string. The basic data type is accessed by value, which means that we can manipulate the actual value stored in the variable

1. Values of basic data types are immutable

No method can change a basic type of value, such as a string:

Var name = "change"; name.substr (); / / hangconsole.log (name); / / changevar s = "hello"; s.toUpperCase () / / HELLO;console.log (s) / / hello

Through these two examples, we will find that the value of the originally defined variable name has never changed, but a new string is returned after calling the substr () and toUpperCase () methods, which has nothing to do with the previously defined variable name.

Some people may have the following questions, look at the code:

Var name = "change"; name = "change1"; console.log (name) / / change1

So it looks like the value of name "changed" actually, var name = "change", the basic type here is string, that is, "change", the "change" here can not be changed, name is just a pointer to "change", the pointer point can be changed, so you can name = "change1". At this time name points to "change1", similarly, the "change1" here also cannot be changed.

In other words, what you think of as a change here is just a "change in the direction of the pointer."

The basic type here refers to "change", not name, so make a clear distinction.

two。 Basic data types cannot add attributes and methods var p = "change"; p.age = 29 position p.method = function () {console.log (name)}; console.log (p.age) / / undefinedconsole.log (p.method) / / undefined

Through the above code, we know that we cannot add properties and methods to the basic type, and show again that the basic type is immutable

3. The assignment of basic data types is a simple assignment

If you assign a value of the base type from one variable to another, a new value is created on the variable object and then copied to the location assigned to the new variable

Var a = 10 * var b = a * / / 11console.log (b) / / 10

In the above code, the value stored in an is 10. 0. When initializing b with the value of a, the value 10. 0 is also saved in b. But 10 in b and 10 in an are completely independent. A copy of the value knowledge an in b. So these two variables can participate in any operation without affecting each other. As shown below:

4. The comparison of basic data types is the comparison of values var person1 ='{}'; var person2 ='{}'; console.log (person1 = = person2); / / true5. The basic data types are stored in the stack area

If you have the following basic types of variables:

Var name = "jozo"; var city = "guangzhou"; var age = 22

Then its storage structure is shown below:

The stack area includes the identifier and the value of the variable.

Js reference type:

Js in addition to the above basic types is the reference type, can also be said to be an object, such as: Object,Array,Function,Data and so on

1. The value of the reference type is changeable var o = {XRAPR 1}; O.x = 2X bank / change the object o.y = 3 by modifying the attribute value of the object; change the object again and add an attribute to it var a = [1rec 2jue 3]; a [0] = 0trample / an element of the change array a [3] = 4Placement / add an element 2 to the array. Reference types can add properties and methods var person = {}; person.name = "change"; person.say = function () {alert ("hello");} console.log (person.name) / / changeconsole.log (person.say) / / function () {alert ("hello");} 3. The assignment of a reference type is an object reference

Take a look at the following code first:

Var a = {}; var b = a 29console.log a.name = "change"; console.log (a.name) / / change;console.log (b.name) / / changeb.age = 29 position console.log (a.age) / / 29console.log (b.age) / / 29

When a value of a reference type is assigned from one variable to another, a copy of the value of the object stored in the variable is also copied into the space allocated for the new variable. What the reference type holds in the variable is the address of the object in heap memory, so, unlike simple assignments of basic data types, a copy of this value is actually a pointer to an object stored in heap memory. After the assignment operation, both variables hold the same object address, and the two addresses point to the same object. Therefore, changing any of these variables will affect each other.

Their relationship is shown below:

Therefore, the assignment of the reference type is actually the assignment of the object stored in the stack address pointer, so the two variables point to the same object, and any operation will affect each other.

4. Comparison of reference types is the comparison of references var person1 = {}; var person2 = {}; console.log (person1 = = person2) / / false

Why do two objects look the same, but not equal?

Because the comparison of reference types is the comparison of references, in other words, it is to compare whether the addresses of two objects that point to heap memory in the stack area are the same, at this time, although p1 and p2 look like a "{}", but the addresses they keep in the stack area pointing to heap memory are different, so the two objects are not equal.

5. Reference types are saved in both the stack area and the heap area

The storage of reference types needs to be done in both the stack area and the heap area of memory, which stores the variable identifier and the address pointing to the heap memory.

If you have the following objects:

Var person1 = {name: "change1"}; var person2 = {name: "change2"}; var person3 = {name: "change3"}

The three objects are saved in memory as shown in the following figure:

Basic packaging type (packaging object):

Let's take a look at the following code:

Var S1 = "helloworld"; var S2 = s1.substr (4)

As we mentioned above, strings are basic data types and there should be no methods, so why can S1 call substr () here?

By reading chapter 3.6 of the authoritative guide to js and chapter 5.6 of advanced programming, we know that ECMAScript also provides three special reference types, Boolean,String,Number. We call these three special reference types basic wrapper types, also known as wrapper objects.

That is to say, when reading the three basic data types, string,boolean and number, the background will create a corresponding basic wrapper type object, so that we can call some methods to manipulate the data.

So when the second line of code accesses S1, the background automatically does the following:

Create an instance of type String; / / var S1 = new String ("helloworld")

Call the specified method in the instance; / / var S2 = s1.substr (4)

Destroy this instance; / / S1 = null

Because of the destruction in step 3, you should be able to understand why basic data types cannot add properties and methods, which is the main difference between basic package types and reference types: object lifetime. Instances of reference types created using the new operator are kept in memory until the execution flow leaves the current scope. Objects of the basic wrapper type, on the other hand, are automatically created only at the moment of execution of a line of code and then destroyed immediately.

On the difference between JavaScript basic data types and reference types is shared here, I hope the above content can be of some help to 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