[JS advanced] Basic type reference type simple Assignment object reference

Source: Internet
Author: User

The ECMASCIRPT variable has two different data types: 基本类型,引用类型 . There are other terms, such as 原始类型和对象类型 , 拥有方法的类型和不能拥有方法的类型 can also be divided into 可变类型和不可变类型 , in fact, these names are based on these two kinds of 类型特点 naming, we love to call what is called O (╯-╰) O.

1. Basic types

The basic data types are: ' Undefined,boolean,number,string,null. Basic types of access are accessed by value, that is, you can manipulate the actual values that are saved in the variable. The basic types have the following characteristics:

1. The value of the base type is not to become:

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

var name = ‘jozo‘;name.toUpperCase(); // 输出 ‘JOZO‘console.log(name); // 输出 ‘jozo‘

It will be found that the original name has not changed, but instead the toUpperCase () method is called to return a new string.
Let's look at one more:

var person = ‘jozo‘;person.age = 22;person.method = function(){//...};console.log(person.age); // undefinedconsole.log(person.method); // undefined

The above code shows that we cannot add properties and methods to the base type, and once again the basic type cannot become;

2. Comparison of basic types is a comparison of values:

They are equal only when their values are equal.
But you might be like this:

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

Aren't they equal? In fact, this is the 类型转换 == knowledge of operators, that is, when you use = = to compare two different types of variables will be some type conversion. A comparison like the one above will first convert true to the number 1 and then the number 1, and the result is true. This is when the = = operator does the type conversion when comparing the two value types, but even = = = = = = = = = = = = = = = = = = = = = = = = = When the two values are of the same type.

var a = ‘jozo‘;var b = ‘jozo‘;console.log(a === b);//true

3. Basic types of variables are stored in the stack (stack memory in memory)

If there are several basic types of variables:

var name = ‘jozo‘;var city = ‘guangzhou‘;var age = 22;

Then its storage structure is as follows:

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

2. Reference types

Reference types can be fun and fun.

JavaScript In addition to the above basic type (number,string,boolean,null,undefined) is a reference type, it can be said to be an object. Objects are collections of properties and methods. That is, a reference type can have properties and methods, and a property can contain both a base type and a reference type. Take a look at some of the characteristics of reference types:

1. The value of a reference type is variable

We can add properties and methods for reference types, or delete their properties and methods, such as:

var person = {};//创建个控对象 --引用类型person.name = ‘jozo‘;person.age = 22;person.sayName = function(){console.log(person.name);} person.sayName();// ‘jozo‘delete person.name; //删除person对象的name属性person.sayName(); // undefined

The above code shows that reference types can have properties and methods, and can be dynamically changed.

2. The value of a reference type is an object that is stored in both the stack memory and the heap memory

JavaScript differs from other languages in that it does not allow direct access to the in-memory location, which means that the object's memory space cannot be manipulated directly, so what do we do? In fact, it is a reference to an operand, so the value of the reference type is accessed by reference.
To be precise, the storage of reference types requires that the stack and heap area of memory (the heap is the heap memory in memory) is done together, the stack memory holds the variable identifier and the pointer to the object in heap memory, or the address of the object in the heap memory.
If there are several objects:

var person1 = {name:‘jozo‘};var person2 = {name:‘xiaom‘};var person3 = {name:‘xiaoq‘};

The three objects that are stored in memory are as follows:

3. Comparison of reference types is a reference comparison

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

The basic type of comparison mentioned above when the two comparison value of the same type, equivalent to the use of = = =, so the output is true. Look again:

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

Perhaps you have seen the flaw, the above comparison is two strings, and the following comparison is two objects, why the long identical objects are not equal?

Remember that reference types are accessed by reference, in other words, if the address in the heap memory of the two objects is the same, it is clear that Person1 and Person2 have different addresses in the heap memory:

So these two are completely different objects, so return false;

3. Simple Assignment

When a base type is assigned a value from one variable to another, a new value is created on the variable and then copied to the location assigned to the new variable:

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

At this point, a holds a value of 10, when you use a to initialize B, the value saved in B is also 10, but B in 10 and a is completely independent, the value is only a copy of the value in a, after which, these two variables can participate in any operation without affecting each other.

That is, after the assignment operation, the base type has two variables that are not affected by one another.

4. Object references

When a value of a reference type is assigned to another variable from one variable, the value of the object stored in the variable is also copied into the space allocated for the new variable. When referring to a reference type, it is mentioned that the object stored in the variable is an address in the heap memory, so, unlike a simple assignment, a copy of this value is actually a pointer to an object stored in the heap memory. Then after the assignment operation, two variables hold the same object address, the two variables point to the same object. Therefore, changing any of these variables will affect each other:

var a = {}; // a保存了一个空对象的实例var b = a;  // a和b都指向了这个空对象a.name = ‘jozo‘;console.log(a.name); // ‘jozo‘console.log(b.name); // ‘jozo‘b.age = 22;console.log(b.age);// 22console.log(a.age);// 22console.log(a == b);// true

Their relationship is as follows:

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

[JS advanced] Basic type reference type simple Assignment object reference

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.