Home  >  Article  >  Web Front-end  >  Method to determine whether a variable is of type Array_javascript skills

Method to determine whether a variable is of type Array_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:22:281148browse

Many times, we need to determine the array type of a variable. How to determine whether a variable is of array type in JavaScript? I recently researched it and shared it with you. I hope it will be helpful to everyone.

Methods to detect objects in JavaScript
1.typeof operator
This method is stress-free for some commonly used types , such as Function, String, Number, Undefined, etc., but it will not work if you detect Array objects.

Copy code The code is as follows:

alert(typeof null); // "object"
alert(typeof function () {
return 1;
}); // "function"
alert(typeof 'Menglong Station'); // "string"
alert( typeof 1); // "number"
alert(typeof a); // "undefined"
alert(typeof undefined); // "undefined"
alert(typeof []); // " object"

2. instanceof operator
This operator is somewhat related to object-oriented in JavaScript. To understand this, you must first understand object-oriented in JavaScript. Because this operator detects whether the prototype chain of the object points to the prototype object of the constructor.
var arr = [1,2,3,1];
alert(arr instanceof Array); // true
3. The constructor attribute of the object
Except instanceof, Each object also has a constructor attribute, which seems to be used to make Array judgments.
Copy code The code is as follows:

var arr = [1,2,3,1] ;
alert(arr.constructor === Array); // true

The second and third methods seem to be impeccable, but in fact there are still some loopholes. When shuttling back and forth in a frame, these two methods are the best. Since each iframe has its own execution environment, objects instantiated across frames do not share prototype chains with each other, thus causing the above detection code to fail!
Copy code The code is as follows:

var iframe = document.createElement('iframe'); //Create iframe
document.body.appendChild(iframe); / /Add to body
xArray = window.frames[window.frames.length-1].Array;
var arr = new xArray(1,2,3); // Declare array [1,2, 3]
alert(arr instanceof Array); // false
alert(arr.constructor === Array); // false

Detect array type method
The above methods seem to be impeccable, but there will be some problems after all. Next, I will provide you with some relatively good methods, which can be said to be impeccable.
1.Object.prototype.toString
The behavior of Object.prototype.toString: first, get an internal property [[Class]] of the object, and then based on this property, return a value similar to The string of "[object Array]" is used as the result (those who have read the ECMA standard should know that [[]] is used to represent attributes used internally in the language and not directly accessible from the outside, called "internal attributes"). Using this method, combined with call, we can obtain the internal attributes [[Class]] of any object, and then convert the type detection into string comparison to achieve our purpose.
Copy code The code is as follows:

function isArrayFn (o) {
return Object. prototype.toString.call(o) === '[object Array]';
}
var arr = [1,2,3,1];
alert(isArrayFn(arr));/ / true

call changes the this reference of toString to the object to be detected, returns the string representation of this object, and then compares whether the string is '[object Array]' to determine whether it is an instance of Array. Why not directly o.toString()? Well, although Array inherits from Object, it will also have a toString method, but this method may be rewritten and fail to meet our requirements, and Object.prototype is the butt of a tiger, which is very Few people dare to touch it, so its "purity" can be guaranteed to a certain extent:)

JavaScript standard document defines: The value of [[Class]] can only be one of the following strings: Arguments , Array, Boolean, Date, Error, Function, JSON, Math, Number, Object, RegExp, String.
This method is often very useful when identifying built-in objects, but please do not use this method for custom objects.
2.Array.isArray()
ECMAScript5 officially introduced Array.isArray() into JavaScript, with the purpose of accurately detecting whether a value is an array. IE9, Firefox 4, Safari 5, Opera 10.5 and Chrome all implement this method. However, versions before IE8 are not supported.
3. Better reference
Based on the above methods, there is a current best way to write an array:
Copy the code The code is as follows:

var arr = [1,2,3,1];
var arr2 = [{ abac : 1, abc : 2 }];
function isArrayFn(value){
if (typeof Array.isArray === "function") {
return Array.isArray(value);
}else{
return Object.prototype.toString.call(value) === "[object Array]";
}
}
alert(isArrayFn(arr));// true
alert(isArrayFn( arr2));// true

How to determine whether a variable is an array type in JavaScript? The above is the method I shared with you to determine whether a variable is an array type in JavaScript. I hope it can Helpful to everyone.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn