1024programmer JavaScript JS basic type and reference type value

JS basic type and reference type value

Introducing concepts: basic types and reference types

1. You can feel that JS variables are extremely loose, so , it is the loose nature of JS variables that determines: JS variable name is just a name used to save a specific value at a specific time, that is to say, the value of the variable and its data type can be changed during the life cycle of the script , although this feature looks interesting and powerful, JS variables are actually more complicated.

2. ECMAScirpt variables have two different data types: basic type and reference type, and there are other names, such as: primitive type and object type, type with method and type that cannot have method Type, etc./0

3. When assigning a value to a variable, the parser must determine whether the value is a basic type value or a reference type value

Basic types refer to simple data segment, while reference types refer to objects that may be composed of multiple values

Basic types: undefined, null, string, number, boolean, symbol (ES6)

Reference types: Object, Array, RegExp, Date, Function

The difference between the two types

Storage:

The value of the basic type is to store In the stack area, that is, the stack memory in the memory

If there are the following variables:

var name = 'jozo'jozo&#  39;;
 var city = 'guangzhou';
 var age = 22;

Then their storage structure is as follows: (the stack area includes the identifier and value of the variable)

The value of the reference type is

If there are the following objects stored in both stack memory and heap memory:

var person1 = {name:'jozo&  #39;};
 var person2 = {name:'xiaom'};
 var person3 = {name:'xiaoq'};

Then their storage structure is as follows:

Access:

Values ​​of primitive types are accessed by value, because the actual value stored in the variable can be manipulated.

The value of the reference type is accessed by reference, because the value of the reference type is an object stored in memory, and unlike other languages, Javascript does not allow direct access to the location in memory, that is, it does not The memory space of the object can be directly manipulated, so when manipulating the object, it is actually manipulating the reference of the object instead of the actual object.

Dynamic properties:

For reference type values, obviously, we can add, change, delete properties and methods:

var person = new Object();
 person.name = "Ozzie";
 console.log(person.name); //"Ozzie"

In the above process, we created an object and added a property to it, if the object is not destroyed or the property is not deleted, Then this property will always exist

However, we cannot add methods and properties to basic types of values

var name = "Ozzie  ";
 name.age = 19;
 consoe.log(name.age); //undefined

Although this operation will not report an error, it will still be silently hung up

Comparison:

Comparisons of primitive types are comparisons of values:

Example:

var a = 1;

var b = true;

console .log(a == b); //true

Comparisons of reference types are comparisons of references:

Example:

var person1 = {};
 var person2 = {};
 console.log(person1 == person2); //false

As mentioned above, the reference type is accessed by reference, in other words, it is to compare whether the addresses in the heap memory of the two objects are the same , obviously, not the same memory location:

Copy variable value:

Copy a value of a basic type to another variable, then, will create a new value on the new variable, and then copy that value to the location assigned to the new variable:

Example:

var a = 10;
 var b = a;
 a++;
 console. log(a); // 11
 console.log(b); // 10

a is completely independent of b, the value is just a copy of the value in a.

After the assignment operation of the basic type, the two variables are not affected by each other:

Then, when copying the value of the reference type, it will also Copy a copy of the value stored in the object to the space of the new variable. The difference is that the copy of the value is actually a pointer to the object stored in the heap. That is, after the copy is complete, the two variables will refer to the same object.

For example:

var a = {}; // a saves an instance of an empty object
 var b = a; // both a and b point to this empty object
 a.name = 'jozo';
 console.log(a.name); // & # 39; jozo & # 39;
 console.log(b.name); // & # 39; jozo & # 39; 

Changing one of the variables will affect the other variable

Passing parameters:

Remember, although there are two ways of accessing variables by value and by reference, the parameters of all functions in ECMAScript are passed by value, that is, the parameter It can only be passed by value, that is to say, copying the value outside the function to the parameter inside the function is similar to copying the value between variables

The transfer of the value of the basic type is like the variable of the basic type A copy of , the passed value will be�Assign a value to a local variable (that is, a named parameter, using the concept in ECMAScript, it is an element in the arguments object), and I will not repeat it here…

However, when passing a value of reference type to a parameter, What is copied to the local variable is the address in memory, so the change of this local variable will be reflected outside the function.

For example:

function setName(obj){
       obj.name = "Ozzie";
 }
 var person = new Object();
 setName(person);
 console.log(person.name); //"Ozzie"

We can see that inside the function, obj and person refer to the same object, in other words, even if this variable is by value Passed, obj will also access the same object by reference, because there is only one object pointed to by person in the heap memory, and it is a global object.

Many people will mistakenly think that parameters are passed by reference, because parameters modified in the local scope will be reflected in the global scope, OK, then let’s look at another example:

p>

function setName(obj){
       obj.name = "Ozzie";
       obj = new Object();
       obj.name = "Nicholas"
 }
 var person = new Object();
 setName(person);
 console.log(person.name); //Ozzie

If the parameter is passed by reference, then obviously the person object will automatically modify the name attribute to Nicholas inside the function, but the result is still Ozzie, which is It shows that even if the value of the parameter is modified inside the function, the original reference remains unchanged. In fact, when obj is rewritten inside the function, the reference of this variable is a local object, and this local object is executed after the function are destroyed immediately thereafter.

Recommended tutorial: “PHP Tutorial”

The above is the detailed content of JS basic types and reference type values. For more information, please pay attention to other related articles on 1024programmer.com!

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/js-basic-type-and-reference-type-value/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索