fe-interview icon indicating copy to clipboard operation
fe-interview copied to clipboard

如何只用两行代码实现判断js中所有数据类型

Open artdong opened this issue 5 years ago • 1 comments

artdong avatar May 11 '20 06:05 artdong

判断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"

artdong avatar May 11 '20 07:05 artdong