How to Check If it is Array?

Got some instruction about how to check if a variable is Array in JavaScript from this book.

Here I just copy and record sth to make me more sensitive for how to check Array in JavaScript.

In the environment which support ES5, just use Array.isArrary(). For example:

Array.isArray([]); // true

// trying to fool the check
// with an array-like object
Array.isArray({
length: 1,
0: 1,
slice: function () {}
}); // false

In somewhere that no ES5 support, just do this:

if (typeof Array.isArray === undefined) {
Array.isArray = function (arg) {
return Object.prototype.toString.call(arg) === '[object Array]';
};
}

赵彪原创,请随意转载,但务必保留作者署名和原文链接