function is(type, obj) {
var clas = Object.prototype.toString.call(obj).slice(8, -1);
return obj !== undefined && obj !== null && clas === type;
}
is('String', 'test'); // true
is('String', new String('test')); // true
In the above example, Object.prototype.toString gets called with the value of this being set to the object whose [[Class]] value should be retrieved.
本文介绍了一个JavaScript函数,用于检查对象是否为指定类型。通过利用Object.prototype.toString方法,此函数能够准确判断传入对象的具体类型。
8890

被折叠的 条评论
为什么被折叠?



