Basic javascript types and javascript

Source: Internet
Author: User

Basic javascript types and javascript

Js contains a total of five original values, six typeof which can be judged, and nine native built-in constructors.

These 569 forms the basis of the js language.

The five original values are: Numbers, characters, Boolean, null, undefined

Typeof can be judged as numbers, characters, Boolean, object, function, and undefined. Note that both null and array are output by tyopeof.

Typeof cannot distinguish between arrays and objects. How can we determine the type? Use Object. prototype. toString. apply ().

If (value & typeof value = 'object' & value. constructor = Array)

The above detection will show false if the array is created in different frames and windows, and the window object is different.

The reliable method is if (Object. prototype. toString. apply (value) = "[object Array]")

The arguments array is not an array, but an object with the length member attribute.
As shown in the following example, arguments is not a common array

Copy codeThe Code is as follows:
Var a = function (){
Var B = Object. prototype. toString. apply (arguments );
Console. log (B );

}
A (); // output [object Arguments]

Copy codeThe Code is as follows:
Var a = function (){
Var c = [];
Var B = Object. prototype. toString. apply (c );
Console. log (B );

}
A (); // output [object Array]

How does instanceof determine whether an instance is used?

The attributes in prototype include constructor.

The default prototype attribute is an object. It can be set to any complex value, and the ignore attribute is set to the original value.

Although he is an object, he is special. The circular chain links each instance to the prototype attribute of its constructor. There is a hidden link between the instance and the prototype attribute of the constructor. This is the _ proto _ of the instance __. At the same time, the constructor attribute of the instance is obtained through constructor of prototype.

However, the constructor should be retained, so that the new instance can have the constructor attribute, or the instanceof can be used for determination.

Copy codeThe Code is as follows:
Var Foo = function (){}

Foo. prototype = {constructor: Foo}

Var FooInstance = new Foo;

FooInstance. _ proto __== Foo. prototype; // true

FooInstance. constructor === Foo; // true

In fact, the instanceof judgment is not based on constructor, but on the prototype chain, as shown in the following example.

Copy codeThe Code is as follows:
Var Foo = function (){};
Foo. prototype = {};
Var FooInstance = {};
FooInstance. _ proto __= Foo. prototype;
Console. log (FooInstance instanceof Foo); // true

Use the original value instead of the constructor.

Which values are false: false, "", null, 0,-0, NaN, undefined, which are false and others are true?

Note the following example.

Copy codeThe Code is as follows:
Var a = Boolean (false );
Var B = new Boolean ("");
If (a) {console. log (a);} // unable to output
If (B) {console. log (B);} // Boolean {[[PrimitiveValue]: false} a new object is equivalent to an object, so it is not false.

The above Article is slightly more theoretical, but these are the basis of the javascript language, you must understand clearly.

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.