白沟网站建设海南网站建设
Object.prototype.toString()能够很好的判断数据的类型及内置对象
- typeof xxx:能判断出number,string,undefined,boolean,object,function(null是object)
- Object.prototype.toString.call(xxx):能判断出大部分类型
- Array.isArray(xxx):判断是否为数组
var test= Object.prototype.toString;
console.log(test.call(1));//[object Number]
console.log(test.call("1"));//[object String]
console.log(test.call(false));//[object Boolean]
console.log(test.call({p:1}));//[object object]
console.log(test.call(undefined));//[object undefined]
console.log(test.call(null));//[object null]
console.log(test.call(function(){}));//[object Function]
console.log(test.call([1,2,3,4]));//[object Array]
console.log(test.call(new Date()));//[object Date]
console.log(test.call(new RegExp()));//[object RegExp]
这里顺便提一下它的兄弟函数vaul