Raw values and complex values in JavaScript _javascript tips

Source: Internet
Author: User

Front.

The data type of JavaScript can be divided into two types: the original type and the reference type. The original type is also known as a basic or simple type, and JavaScript basic data types include undefined, Null, Boolean, number, and string five, while reference types are also called complex types, which are object in JavaScript. Corresponding to this, their values are also called original values and complex values, respectively.

Characteristics

Original value (primitive value)

Simply put: The original value is a fixed and simple value, which is a simple data segment stored in stacks (stack), that is, their values are stored directly in the location where the variable is accessed.

The original value is the lowest form or simplest form of data or information that is available in JavaScript. The values of the original type are called original values because they are not fine-grained. In other words, numbers are numbers, characters are characters, Boolean values are true or false,null and undefined are null and undefined. The values themselves are simple and cannot represent values made up of other values

What types are the original types?

The original type (primitive type) has the following five types: undefined,null,boolean,number,string

We can use TypeOf to determine whether one is within the range of a type.

typeof operator

Using the typeof operator on a variable or value returns one of the following values:

Attention:

1. The return value is a string type.

2. Compared to the original type, there is also a null, which is special, using typeof (NULL), which returns "Object", we interpret null as a placeholder for object.

Complex value

Complex values can consist 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, not a specific known value

Storage mode

Stack storage

Because the original value occupies a fixed space, is a simple data segment, in order to facilitate the elevation of variable query speed, store it in stack (stack)

Heap Storage

Because the size of the complex value will change, it can not be stored in the stack, otherwise it will reduce the speed of the variable query, so it is stored in the heap (heap), stored at the variable value is a pointer to the storage object of memory

How to access

Access by value

The original values are stored and manipulated as the values that cannot be refined, and references to them transfer their values

var myString = ' foo ';
var mystringcopy = myString;
var myString = null;

Reference access

Complex values are stored and manipulated by reference, rather than actual values. When you create a variable that contains a complex object, the value is a reference address in memory. When a complex object is referenced, its name (that is, a variable or object property) is used to get the value of the object through an in-memory reference address

var myObject = {};
var copyofmyobject = myobject;//does not replicate the value, but instead copies the reference
Myobject.foo = ' bar ';//operation MyObject value
// Now if you output MyObject and copyofmyobject, you will output the Foo property because they refer to the same object

Comparison method

The original value is compared with the value, while the complex value uses a reference comparison. Complex values are equal only if they refer to the same object (that is, the same address). Even two variables that contain the same object are not equal to each other because they do not point to the same object

var price1 = ten;
var price2 = ten;
var price3 = new Number (' Ten ');
var price4 = Price3;
Console.log (Price1 = = Price2);//true
console.log (Price1 = price3);//false
price4 = ten;
Console.log (Price4 = = Price3);//true
console.log (Price4 = = Price3);//false 
var objectfoo = {same: ' same '};
var Objectbar = {same: ' same '};
Console.log (Objectfoo = = Objectbar);//false
var objecta = {foo: ' Bar '};
var OBJECTB = objecta;

Dynamic Properties

For complex values, you can add properties and methods to them, as well as change and delete their properties and methods, but simple values can not add properties and methods

Complex values support dynamic object properties Because we can define an object, then create a reference, update the object, and all variables that point to that object will be updated. 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 have as many references as needed, and they always point to the same object even if they change

var str = ' Test ';
Str.property = true;
Console.log (str.property);//undefined 
var obja = {property: ' value '};
var pointer1 = Obja;
var pointer2 = pointer1;
Obja.property = null;

Packing type

When the original value is used as an object created by the constructor, JavaScript converts it to an object so that it can use the object's attributes and methods, and then discard the object's nature and return it to its original value

Related Article

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.