使用方法
Array.isArray是用来判断一个值是否是数组的方法。
Array.isArray([1, 2]) // trueArray.isArray(1) // false
模拟实现
使用Object.prototype.toString判断类型,返回true or false
/*** 工具方法*/const getType = value => {const objectType = Object.prototype.toString.call(value);const type = objectType.match(/^\[object (.*)\]$/)[1];return type.toLowerCase()}// isMyArrayArray.isMyArray = function(value) {return getType(value) === 'array';}
扩展
可根据Object.prototype.toString实现一个获取数据类型的方法
const getType = value => {const objectType = Object.prototype.toString.call(value);const type = objectType.match(/^\[object (.*)\]$/)[1];return type.toLowerCase()}
