标签:return 方法 ret turn cto isa ice str array
1、简单的判断,typeof与constructor
(1)typeof
typeof(1); //number typeof("s"); //string typeof(a); //undefined typeof(true); //boolean typeof(a); //function typeof(a); //object(对象、null、数组)
(2)constructor
console.log([].constructor == Array); console.log({}.constructor == Object); console.log("string".constructor == String); console.log((123).constructor == Number); console.log(true.constructor == Boolean);
2、数组的判断
(1)严谨类型判断
function isArray(object){ return object && typeof object===‘object‘ && Array == object.constructor; }
(2)特性判断
function isArray(object){ return object && typeof object===‘object‘ && typeof object.length===‘number‘ && typeof object.splice===‘function‘ && //判断length属性是否是可枚举的 对于数组 将得到false !(object.propertyIsEnumerable(‘length‘)); }
(3)最简单判断方法
function isArray(o) { return Object.prototype.toString.call(o) === ‘[object Array]‘; }
标签:return 方法 ret turn cto isa ice str array
原文地址:http://www.cnblogs.com/huangqiming/p/7084148.html