fe-interview
fe-interview copied to clipboard
如何只用两行代码实现判断js中所有数据类型
判断js数据类型最简方法
let _toString = Object.prototype.toString;
function getType (value) {return _toString.call(value).slice(8, -1)}
getType(); // "Undefined"
getType(''); // "String"
getType(1); // "Number"
getType(1n); // "BigInt"
getType(Symbol('uid')); // "Symbol"
getType(true); //"Boolean"
getType([]); //"Array"
getType({}); //"Object"
getType(null); //"Null"
getType(function(){}); //"Function"