A re-understanding of variable types in JavaScript

Source: Internet
Author: User

In the Book of JavaScript revelation: In JavaScript, the object is "King" (Almost everything in JavaScript is an object or object).

The most profound understanding of the "swallow" of JavaScript--and one of the most rewarding realizations of all time--is that there are two kinds of JavaScript values-primitive values and objects (a collection of original values). A bit more directly, all can be understood as an object, although the original value is not an object, but the original value has an equivalent wrapper object, according to this, in JavaScript can really say that the object is "King".

One: Original value (or simple value)

1. There are 5 types of raw values: 5 (nummber), "str" (String), True (Boolean), Undefined, null

2. Features: The original value can not be further refined, that is, the values themselves are very simple, can not represent the value of other values.

3. You can create the original values with number (), String (), Boolean (), or you can create complex objects with those constructors. The difference is whether the new keyword is used when calling

  

var a=2,b=number (2), c=new number (2), Console.log (typeof a + "+typeof B +" "+typeof c), Console.log ((a===b) +" "+ (A===c) +" " + (a==c));//number number  object//true false true

Note: It is clear from the above that the data types are not the same when using new and do not use new, and are not equal in the case of strictly equal judgments

4. The 4th is to let us believe that JavaScript is "King": When using literal values for strings, numbers, and Booleans, the actual complex object is created only if the value is treated as an object (this operation is performed on its own in the background, after the object is created, And after the object is used, the object is automatically freed). In other words, the original data type is used until you try to use a method associated with the constructor or retrieve the property (such as Var charactersinfoo= "foo". Length). When this happens, JavaScript creates a wrapper object for the literal value behind the scenes to treat the value as an object. When the method is called, JavaScript discards the wrapper object, which returns the literal type.  This is why strings, numbers, and Booleans are considered primitive (or simple) data types. The difference between "everything in JavaScript is Object" and "Everything in JavaScript is used as object."

5. Comparison of simple values by literal comparison

Two: Complex values

1. Complex values: The Native object Constructor object (), Array (), function (), Date (), Error (), REGEXP () are complex types because they can contain one or more primitive values or complex values

2. Comparison of complex objects using reference comparisons, compared to referenced addresses

A re-understanding of variable types in JavaScript

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.