JS-数据类型及构造函数类型判断

125 阅读1分钟

通用方法(除NaN)

Object.prototype.toString.call()

判断数据类型方法

typeof()

判断构造函数方法

instanceof

判断NaN唯一方法

isNaN()

**********************String***************************
let str = 'str';
// typeof
console.log(typeof(str));  // string
// 对象的class属性来判断
console.log(Object.prototype.toString.call(str)); // [object String]



**********************Object***************************
let obj = {'obj':'obj'};
// typeof
console.log(typeof(obj)); // object
// 原型对象判断
console.log(Object.prototype.isPrototypeOf(obj)); // true
// instanceof
console.log(obj instanceof Object); // true
// 对象的class属性来判断
console.log(Object.prototype.toString.call(obj)); // [object Object]


**********************BigInt***************************
let int = 123456789n;
// typeof
console.log(typeof(int)); // bigint
// 对象的class属性来判断
console.log(Object.prototype.toString.call(int)); // [object BigInt]



**********************Symbol***************************
let sym = Symbol('test');
// typeof
console.log(typeof(sym)); // symbol
// 对象的class属性来判断
console.log(Object.prototype.toString.call(sym)); // [object Symbol]


**********************Boolean***************************
let bo = true;
// typeof
console.log(typeof(bo)); // boolean
// 对象的class属性来判断
console.log(Object.prototype.toString.call(bo)); // [object Boolean]


**********************Number***************************
let num = 123;
// typeof
console.log(typeof(num)); // number
// 对象的class属性来判断
console.log(Object.prototype.toString.call(num)); // [object Number]


**********************NaN***************************
let nan = NaN;
// typeof
console.log(typeof(nan)); // number
// 对象的class属性来判断
console.log(Object.prototype.toString.call(nan)); // [object Number]
// 直接判断
console.log(isNaN(nan)); // true


**********************undefined***************************
let unde = undefined;
// typeof
console.log(typeof(unde)); // undefined
// 对象的class属性来判断
console.log(Object.prototype.toString.call(unde)); // [object Undefined]


**********************null***************************
let nul = null;
// typeof
console.log(typeof(nul)); // object
// 对象的class属性来判断
console.log(Object.prototype.toString.call(nul)); // [object Null]


**********************Function***************************
let fun = function (){};
// typeof
console.log(typeof(fun)); // function
// 原型对象判断
console.log(Function.prototype.isPrototypeOf(fun)); // true
// instanceof
console.log(fun instanceof Function); // true
// 对象的class属性来判断
console.log(Object.prototype.toString.call(fun)); // [object Function]


**********************Array***************************
let arr = [1,2,3];
// typeof
console.log(typeof(arr));
// 原型对象判断
console.log(Array.prototype.isPrototypeOf(arr));
// instanceof
console.log(arr instanceof Array);
// 对象的class属性来判断
console.log(Object.prototype.toString.call(arr));
// 直接判断
console.log(Array.isArray(arr));


**********************Set***************************
let set = new Set();
// typeof
console.log(typeof(set));
// 原型对象判断
console.log(Set.prototype.isPrototypeOf(set));
// instanceof
console.log(set instanceof Set);
// 对象的class属性来判断
console.log(Object.prototype.toString.call(set));


**********************Map***************************
let map = new Map();
// typeof
console.log(typeof(map));
// 原型对象判断
console.log(Map.prototype.isPrototypeOf(map));
// instanceof
console.log(map instanceof Map);
// 对象的class属性来判断
console.log(Object.prototype.toString.call(map));