Review of JS array type judgment and data type judgment

Source: Internet
Author: User

The first two days to participate in the front-end of the telephone interview, which asked the array type of judgment, although previously seen in this aspect of the content, but the time has been a bit vague, the answer is not very good, now on the relevant content to do a review and summary.

Let's start by answering the question: how can I tell if an object is an array?

There are several ways to determine whether an object is an array method:

One, Array.isarray () function

This can be done in ECMAScript5 by Array.isarray ():

Array.isarray ({});    // false Array.isarray ([]);    // true

Second, instanceof

The instanceof operator is used to determine whether a variable is an instance of an object and can only be used in a simple case:

instanceof Array    //trueinstanceof array    //false

instanceof problem: In a Web browser, there may be multiple windows or multiple forms, each with its own JS environment and its own global object, each with its own set of constructors, so that one form object cannot be an instance of a constructor in another form. Confusion in forms does not occur frequently.

Third, prototype and call

Object.prototype.toString.call ([]) = = = "[Object Array]"    //true Object.prototype.toString.call ({}) = = = "[Object Array]"    //false

Iv. Constructor

Constructor returns the constructor of an object

[].constructor==array    //true{}.constructor==array    ///false

Note: Using instanceof and Construtor to determine the array, the array being judged must be declared on the current page. For example, when an array is declared in a sub-page and its value is assigned to a variable of the parent page, the variable is judged: variable. Constructor==array; returns false at this time.

Cause: The array belongs to the reference data, only the reference address is passed during the pass, and each page array native object's referenced address is different, that is, the parent page's array is not equal to the sub-page array.

Other types of judging methods are similar to arrays, and here is a list of the return values for instanceof and constructor:

Variable Variable. constructor typeof variable
[] Array Object
{} Object Object
var f=function () {} Fuction function
"A String" String String
88 Number Number
True Boolean Boolean
var a / Undefined
Null / Object
New User () User Object

Review of JS array type judgment and data type judgment

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.