Js determines whether a variable is an object or an array.

Source: Internet
Author: User

For many times, we do not know how to judge whether a variable is an array or an object. Next I will give you a detailed introduction to js to determine whether a variable is an object or an array instance.

All typeof objects are returned.

In JavaScript, all data types are strictly objects, but we still have different types in actual use. If you want to determine whether a variable is an array or an object uses typeof, because it returns all objects

The Code is as follows: Copy code

Var o = {'name': 'lil '};
Var a = ['reg ', 'Blue'];

Document. write ('o typeof is '+ typeof o );
Document. write ('<br/> ');
Document. write ('a typeof is '+ typeof );

Run:

O typeof is object
A typeof is object

Therefore, we can only discard this method. There are two methods to determine whether an array or object is used.

First, use the typeof plus length attribute
The array has the length attribute, the object does not exist, and both the typeof array and the object return the object, so we can judge this way.

The Code is as follows: Copy code

Var o = {'name': 'lil '};
Var a = ['reg ', 'Blue'];

Var getDataType = function (o ){
If (typeof o = 'object '){
If (typeof o. length = 'number '){
Return 'array ';
} Else {
Return 'object ';
}
} Else {
Return 'Param is no object type ';
}
};

Alert (getDataType (o); // Object
Alert (getDataType (a); // Array
Alert (getDataType (1); // param is no object type
Alert (getDataType (true); // param is no object type
Alert (getDataType ('A'); // param is no object type

Second, use instanceof
You can use instanceof to determine whether a variable is an array, for example:

The Code is as follows: Copy code

Var o = {'name': 'lil '};
Var a = ['reg ', 'Blue'];

Alert (a instanceof Array); // true
Alert (o instanceof Array); // false

You can also determine whether it belongs to an object.

The Code is as follows: Copy code
Var o = {'name': 'lil '};
Var a = ['reg ', 'Blue'];

Alert (a instanceof Object); // true
Alert (o instanceof Object); // true

However, the array also belongs to the object, so both of the above are true. Therefore, we should use instanceof to determine whether the data type is an object or an array first, and finally determine the object.

The Code is as follows: Copy code
Var o = {'name': 'lil '};
Var a = ['reg ', 'Blue'];

Var getDataType = function (o ){
If (o instanceof Array ){
Return 'array'
} Else if (o instanceof Object ){
Return 'object ';
} Else {
Return 'Param is no object type ';
}
};

Alert (getDataType (o); // Object
Alert (getDataType (a); // Array
Alert (getDataType (1); // param is no object type
Alert (getDataType (true); // param is no object type
Alert (getDataType ('A'); // param is no object type

If you do not prioritize Array, for example:

The Code is as follows: Copy code
Var o = {'name': 'lil '};
Var a = ['reg ', 'Blue'];

Var getDataType = function (o ){
If (o instanceof Object ){
Return 'object'
} Else if (o instanceof Array ){
Return 'array ';
} Else {
Return 'Param is no object type ';
}
};

Alert (getDataType (o); // Object
Alert (getDataType (a); // Object
Alert (getDataType (1); // param is no object type
Alert (getDataType (true); // param is no object type
Alert (getDataType ('A'); // param is no object type

Then the array is determined as an object.

 

Next, let's take a look at

REFERENCE The ECMA-262 specification to give a general method:

The Code is as follows: Copy code

Function isArray (obj ){
Return Object. prototype. toString. call (obj) = '[object Array]';
}

ECMA-262 explanation:

The Code is as follows: Copy code

Object. prototype. toString () When the toString method is called, the following steps are taken:
Get the [[Class] property of this object.

Compute a string value by concatenating the three strings "[object", Result (1), and "]".
Return Result (2)

This specification defines the Object. prototype. toString behavior: first, obtain an internal attribute of the object [[Class], and then, based on this attribute, returns a string similar to "[object Array]" as the result (we should all know after reading the ECMA standard, [[] is used to indicate the attributes used inside the language and which cannot be accessed externally, is called "Internal attribute "). With this method, combined with call, we can obtain the internal attribute [[Class] of any object, and then convert the type detection to string comparison to achieve our goal.
The description of the Array in the ECMA standard:

The ECMA-262 wrote

The Code is as follows: Copy code

New Array ([item0 [, item1 [,…])
The [[Class] property of the newly constructed object is set to "Array ".


A disadvantage of js prototype inheritance is that the original prototype object of the subclass is replaced. When obtaining the constructor of the subclass, the constructor inherited from the parent class is actually used.
If you do not want to change the constructor, use the following method to implement inheritance:

The Code is as follows: Copy code

Function. prototype. extend = function (parent ){
Var cls = new Function ();
Cls. prototype = parent. prototype;
This. prototype = new cls;
This. prototype. constructor = this;
}
Function t (){};
T. extend (Array );
Alert (x. constructor); // = t

Instaceof is used to determine the object type, for example:

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.