JS IsArray, typeof, Instanceof

Source: Internet
Author: User

Array.isarray ()

Used to verify that arrays are not

        var a = [Console.log]        (typeof a);   // Object        Console.log (Array.isarray (a));   // true

It can be seen that typeof does not validate the array, although Array.isarray () can verify the array, but IE8 is incompatible

        var a = [Object.prototype.toString.call]        alert ((a))  //  [Object Array]

This method can be compatible with IE8 and the following browsers

typeof

        function foo () {}        Console.log (typeof foo);  // function        Console.log (typeofnull//  object        console.log (typeof undefined);  // undefined        Console.log (typeof ' a ');    //   string        console.log (typeof 1);    //  Number

Here null is the basic type, not an object, this is a JS bug. There is also a notable place to watch:

null);  // true

Because undefined is derived from null in JS

instanceof

        function foo () {}         var New foo ()         instanceof foo);   // true

Instanceof is a prototype chain to find, if the display prototype object of function Foo is on the implicit prototype chain of a object, it returns true, otherwise false

The green one is the prototype chain of object A.

JS IsArray, typeof, Instanceof

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.