废话不多说
Object.defineProperty(Object.prototype, '_type', {
get() {
return Object.prototype.toString.call(this).slice(8, -1)
}
})
console.assert((1)._type === 'Number')
console.assert([]._type === 'Array')
console.assert(({})._type === 'Object')
console.assert(''._type === 'String')
console.assert((new Map())._type === 'Map')
console.assert((new Set())._type === 'Set')
console.assert(true._type === 'Boolean')
console.assert((() => { })._type === 'Function')
如果要加上 空类型 建议调用的时候直接使用
Object.prototype.toString.call(null).slice(8, -1) === 'Null' //true
Object.prototype.toString.call(undefined).slice(8, -1) === 'Undefined' //true