- Object.prototype.toString.call();
- 对于 Object 对象,直接调用 toString() 就能返回 [object Object] 。而对于其他对象,则需要通过 call / apply 来调用才能返回正确的类型信息。
function getType(value) {
if(typeof value !== 'object') {
return typeof value;
} else {
return Object.prototype.toString.call(value).split(' ')[1].slice(0, -1);
}
};
console.log( getType() );
console.log( getType(null) );
console.log( getType(123) );
console.log( getType('温情') );
console.log( getType({
}) );
console.log( getType([]) );
console.log( getType(new Date) );
console.log( getType(new RegExp) );
console.log( getType(new Function) );