在 IE6 以下,Object.prototype.toString 会对 null 和 undefined 返回 [object Object] 。并且 null == undefined 。
const type = (function () {
var class2type = {};
"Boolean Number String Function Array Date RegExp Object Error".split(' ').forEach((item, index) => {
class2type[`[object ${item}]`] = item.toLowerCase();
})
return function (obj) {
if (obj == null) {
return obj + '';
}
typeof obj === 'object' || typeof obj === 'function' ? class2type[Object.prototype.toString.call(obj)] || 'object' : typeof obj;
}
})()