The original value and the reference value

In ECMAScript, the value of the variable may be present in two types, i.e., the original value and the reference value.

Original value
Simple data segment stored on the stack (Stack) in, that is, their value is directly stored in a variable accessible location.
Reference value
Objects stored in the heap (heap), that is, the value stored in the variable is a pointer (Point), pointing to memory at a storage object.

When assigning a variable, ECMAScript interpreter must determine whether the value is a primitive type or a reference type. To do this, we need to try to explain the program to determine whether the value is one of ECMAScript primitive types, namely Undefined, Null, Boolean, Number, and String type. Since the space occupied by these primitives is fixed, so they can be stored in a small memory area - the stack. Such stored values ​​facilitates rapid search variable.

In many languages, the string is treated as a reference type, instead of the original type, because the length of the string is variable. ECMAScript break this tradition.

If a value is a reference type, then its storage space allocated from the heap. Due to the size of the reference value will change, so you can not put it in the stack, otherwise it will reduce the speed of the search variables. In contrast, the value of the variable on the stack is the address space of the object stored on the heap. Address the size is fixed, so it is stored on the stack without any negative impact on performance variables. As shown below:

Primitive value and reference value stored in the stack and the stack

Primitive types

As described above, ECMAScript there are five primitive types (primitive type), i.e. Undefined, Null, Boolean, Number, and String. The term & put ECMA-262 type (type) is defined as a set of values, each range defines a primitive type and it contains literal value representation.

ECMAScript provides typeof operator to determine whether or not a value within a range of a certain type. Can use this operator determines whether a value type represents a primitive: If it is a primitive type, it may be determined which represents the original type.

Guess you like

Origin www.cnblogs.com/SallyShan/p/11483288.html