Original and complex values in JavaScript, javascript
Previous
Javascript data types can be divided into two types: original type and reference type. The original type is also known as a basic type or a simple type. The basic javascript data types include Undefined, Null, Boolean, Number, and String. The reference type is also known as a complex type and is an Object in Javascript. Correspondingly, their values are also called original values and complex values.
Features
Primitive value)
Simply put, the original values are fixed and simple values, and are simple data segments stored in the stack. That is to say, their values are directly stored in the variable access location.
The original value is the lowest or simplest form of data or information available in Javascript. Values of the original type are called original values because they cannot be refined. That is to say, the numbers are numbers, the characters are characters, the Boolean values are true or false, and null and undefined are null and undefined. These values are simple and cannot represent values composed of other values.
What types are original types?
Primitive type has the following types: Undefined, Null, Boolean, Number, String
We can use typeof to determine whether a data type is within the specified range.
Typeof Operator
If you use the typeof operator for a variable or value, one of the following values is returned:
Note:
1. the return value is of the string type.
2. There is a difference between null and the original type. This is special. If typeof (null) is used, "object" is returned, and null is interpreted as a placeholder of the object.
Complex Value
Complex values can be composed of many different types of javascript objects. The size of a complex object in memory is unknown, because a complex object can contain any value rather than a specific known value.
Storage Method
Stack Storage
Because the original value occupies a fixed space and is a simple data segment, it is stored in the stack to facilitate variable query speed.
Heap Storage
Because the size of complex values changes, they cannot be stored in the stack. Otherwise, the variable query speed is reduced. Therefore, they are stored in heap, the value stored in the variable is a pointer pointing to the memory of the storage object
Access Method
Access by value
Raw values are stored and operated as non-refined values. referencing them transfers their values.
var myString = 'foo';var myStringCopy = myString;var myString = null;console.log(myString,myStringCopy);//null,'foo'
Reference access
Complex values are stored and operated by reference, rather than the actual values. When creating a variable that contains complex objects, its value is a reference address in the memory. When referencing a complex object, use its name (that is, the variable or object attribute) to obtain the object value through the reference address in the memory.
Var myObject ={}; var copyOfMyObject = myObject; // The reference myObject is copied instead of the copied value. foo = 'bar'; // operation of values in myObject // If myObject and copyOfMyObject are output, the foo attribute is output because they reference the same object console. log (myObject, copyOfMyObject); // Object {foo = "bar "}
Comparison Method
The original values are compared by values, while the complex values are compared by reference. Complex values are equal only when the same object (that is, the same address) is referenced. Even two variables containing the same object are not equal to each other because they do not point to the same object.
var price1 = 10;var price2 = 10;var price3 = new Number('10');var price4 = price3;console.log(price1 == price2);//trueconsole.log(price1 == price3);//falseprice4 = 10;console.log(price4 == price3);//trueconsole.log(price4 === price3);//false var objectFoo = {same:'same'};var objectBar = {same:'same'};console.log(objectFoo == objectBar);//falsevar objectA = {foo: 'bar'};var objectB = objectA;console.log(objectA == objectB);//true
Dynamic attributes
For complex values, you can add attributes and methods to them, or change or delete their attributes and methods. However, you cannot add attributes and methods to simple values.
Complex values support dynamic object attributes, because we can define objects, create references, update objects, and update all variables pointing to the objects. A new variable points to an existing complex object and does not copy the object. This is why complex values are sometimes referred to as reference values. Complex values can be referenced as needed. Even if the object changes, they always point to the same object.
var str = 'test';str.property = true;console.log(str.property);//undefined var objA = {property: 'value'};var pointer1 = objA;var pointer2 = pointer1;objA.property = null;console.log(objA.property,pointer1.property,pointer2.property);//null null null
Packaging type
When the original value is used as an object created by the constructor, Javascript will convert it into an object so that you can use the features and methods of the object, and then discard the object nature, and change it back to the original value.
Articles you may be interested in:
- Reading Notes on JavaScript advanced programming (2) original types in ECMAScript
- Comparison between original js Code and jquery
- Example of an original Ajax request in html + js + php
- Summary of common methods for using the original JS Selector
- Taking width as an example to obtain the original size of an image using JavaScript
- Use js to get the original image size
- Analysis of javascript original values and Object Reference instances