JS determines whether a variable is an object or an array

Source: Internet
Author: User
Tags extend inheritance

typeof all Return Object

All data types in JavaScript are strictly objects, but in practice we still have types, if we want to determine whether a variable is an array or an object using TypeOf, because it all returns object

The code is as follows Copy Code

var o = {' name ': ' Lee '};
var a = [' reg ', ' Blue '];

document.write (' O typeof is ' + typeof O);
document.write (' <br/> ');
document.write (' A typeof is ' + typeof a);

Perform:

O typeof is Object
A typeof is Object

Therefore, we can only discard this method, to determine that there are two methods of array or object

First, use the TypeOf plus length property
The array has a length property, the object is not, and the TypeOf array and object are returned object, so we can judge

The code is as follows Copy Code

var o = {' name ': ' Lee '};
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, using instanceof
Use instanceof to determine whether a variable is not an array, such as:

The code is as follows Copy Code

var o = {' name ': ' Lee '};
var a = [' reg ', ' Blue '];

Alert (a instanceof Array); True
Alert (o instanceof Array); False

or whether it belongs to the object.

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

Alert (a instanceof Object); True
Alert (o instanceof Object); True

But the array is also object, so the above two are true, so we want to use instanceof to judge whether the data type is an object or an array, we should prioritize the array, and finally judge the object

The code is as follows Copy Code
var o = {' name ': ' Lee '};
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 don't prioritize array, for example:

  code is as follows copy code
var o = {' name ': ' Lee '};
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 also judged to be object.

Here's a look at a foreign man.

Refer to the ECMA-262 specification to a generic method:

The code is as follows Copy Code

function IsArray (obj) {
return Object.prototype.toString.call (obj) = = ' [Object Array] ';
}

ECMA-262 explains:

The code is as follows Copy Code

Object.prototype.toString () when the "toString" 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)

The specification defines the behavior of the Object.prototype.toString: first, get an internal property of an object [[Class]], and then, based on this property, returns a similar to "[Object Array]" string as a result (read the ECMA standard should know that [[]] is used to represent an externally inaccessible property, called an "internal property," used within a language.) Using this method, and then with call, we can get the internal properties [[Class]] of any object, and then convert the type detection into string comparisons to achieve our goal.
Description of array in the ECMA standard:

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".


JS prototype inheritance has a disadvantage, that is, the original prototype object is replaced, in the acquisition of the subclass of the constructor, actually take from the parent class inherited constructor.
If you do not want to change constructor, you can implement inheritance in the following ways:

The code is as follows Copy Code

Function.prototype.extend = function (parent) {
The 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

And instaceof is the type of object to judge, such as:

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.