function getType(data) { const type = Object.prototype.toString.call(data); let res = ''; switch (type) { case '[object Object]': res = 'Object'; break; case '[object Array]': res = 'Array'; break; case '[object String]': res = 'String'; break; case '[object Number]': res = 'Number'; break; case '[object Undefined]': res = 'Undefined'; break; case '[object Null]': res = 'Null'; break; case '[object Function]': res = 'Function'; break; case '[object Boolean]': res = 'Boolean'; break; case '[object Symbol]': res = 'Symbol'; break; case '[object BigInt]': res = 'BigInt'; break; default: break; }return res;
}
console.log('getType({}): ', getType({})); console.log('getType([]): ', getType([])); console.log('getType("123"): ', getType('123')); console.log('getType(123): ', getType(123)); console.log('getType(): ', getType()); console.log('getType(null): ', getType(null)); console.log('getType(()=>{}): ', getType(()=>{})); console.log('getType(true): ', getType(true)); console.log('getType(Symbol()): ', getType(Symbol())); console.log('getType(BigInt(123)): ', getType(BigInt(123)));
标签:object,console,log,res,getType,数据类型,js,break,准确 From: https://www.cnblogs.com/djjlovedjj/p/17813370.html