How to understand the basic types (primitive types) of data and reference data types

Introduction: value / string / are substantially Boolean type, object / array / function is a reference type

6 ES5 in data types, namely:. Number / string / boolean / null / undefined / object where the object is a broad object can be further subdivided into: array / function / object and the like. 

typeof(1); // "number"
typeof("1"); // "string"
typeof(true); // "boolean"
typeof({}); // "object"
typeof(null); // "object"
typeof(undefined); // "undefined"

// 数组也是对象
typeof([1,2,3]); // "object"

// 函数也是对象
var aFunc = function() {};
typeof(aFunc); // "function";
aFunc instanceOf Object; // true

Which, number / string / boolean primitive type, also known as: primitive types; and the object is a reference type, where the null and undefined bit special, whether it temporarily;

 

note: 

1. Although typeof (null) for the "object", but: null instanceOf Object to false.

2. typoof(undefined) 为 "undefined";

 

Step: basic types of data stored in the stack memory

What is the stack memory? We can now simply understood as is the case following the process in obtaining data stack memory is based on the variable name, find the corresponding memory address, and then take out the data memory address.

 

That is, data can not stack memory or a memory address, can only be a certain value, which in fact we are talking about the basic types such as the following code, we declare called:. Name of the variable, this time in memory a memory address will be the same as the name binding, then we assign a value to a string: "hanmeimei", then memory will create a string data: "hanmeimei", and the memory address where this data. " bound "to the variable name name. this realization of the assignment.

When we modify this value "lilei", it is actually a modified memory address pointed to by name, then name will no longer point "hanmeimei" memory address is located.

// declare a variable, because the variable has no value, and therefore point to undefined; 
var name; 

// assignment, the memory address of the newly created "hanmeimei" data bound to the name, name no longer points to undefined; 
name = "hanmeimei" ; 

// "Lilei" memory address where the data memory pointed to by name in the newly created, no longer refers to "hanmeimei" where the memory address 
name = "lilei";

The above process has gone through the following process:  

 

 

 

note: 

1. JavaScript in everything is an object, basic types, too, but they object characteristics reflected in their respective wrapper objects.

2. The actual effect of the described process is complex than FIG here only for demonstration;

3. If the data is not pointed to any variable, then this data will be destroyed garbage collection, the memory will be released;

 

Step Two: reference types of data stored in the heap memory

Heap memory is the memory of another data organization, unlike the stack variables in memory => memory address => this reference data, stored in the heap memory or data memory address, that is to say: the heap memory to continue to take data to find data in the memory address pointed to, if still a memory address, then keep looking down such data in the array of the following is the most simple heap memory: 

var family, son;

son = "liqiang";

family = ["lilei", "hanmeimei", son];

对应的演示图如下: 

 

 

 

注意: 

1. 基本类型无法被修改, 只能被覆盖, 原因是基本类型指向的内存地址里面的数据无法被修改, 只能新建一个数据, 把变量名指向这个内存地址;

2. 原始类型可以被修改, 指的是它指向的内存地址里面的内存地址可以被修改, 比如上面的000009地址里面的数据, 完全可以再加其他的. 不过加的也只能是内存地址;

3. 类似String.prototype.replce()一类的类型原始的方法, 产生的数据都是在一个新的内存地址上, 因为它无法做到改变自身.

 

Guess you like

Origin www.cnblogs.com/aisowe/p/11586433.html