关于 Object.prototype.toString.call() 方法

Javascript中可通过typeof来获取对象的类型,但是对象如果是内置的继承Object的类型,typeof也只能返回object,不能获取对象的具体类型,如Date,Array,Boolean,Number,String,RegExp,ERROR,对他们应用typeof均返回object,但是

Object.prototype.toString.call(obj) 能够返回对象的具体类型,如下所示:

var toString = Object.prototype.toString;
console.log(toString.call(new Date())); // 输出[object Date]
console.log(toString.call(new Array())); // 输出[object Array]
console.log(toString(new Boolean())); // 输出[object Boolean]
console.log(toString(new Number())); // 输出[object Number]
console.log(toString(new String())); // 输出[object String]
console.log(toString(new RegExp())); // 输出[object RegExp]
console.log(toString(new Error())); // 输出[object Error]

 

posted @ 2015-12-10 22:44  比昂  阅读(176)  评论(0编辑  收藏  举报