/**
* todo typeof、instanceof、constructor类型判断
*/
//* typeof只能判断基本类型,数组、对象等都是Object
console.log(typeof 3 === 'number'); // true
console.log(typeof '3' === 'string'); // true
console.log(typeof false === 'boolean'); // true
console.log(typeof NaN === 'number'); // true
console.log(typeof undefined); // undefined
console.log(typeof {}); // object
console.log(typeof function() {}); // function
console.log(typeof []); // object
console.log(typeof new Set().add(3)); // object
console.log(typeof Symbol); // funtion
console.log(typeof new Map()); // object
console.log(typeof new WeakMap()); // object
console.log(typeof new Function()); // funtion
//* constructor 是从原型上查看对方是什么类型,基本类型(除字符串)都不能判断 --但能改变原型
console.log([].constructor === Array) // true
console.log({}.constructor === Object) // true
console.log('s'.constructor === String) // true
function fn() {}
console.log(fn.constructor === Function)
// 改变原型指向
fn.prototype = Object.prototype
console.log(fn.constructor === Function)
/**
*! instanceof 检测当前实例是否属于这个类的
* + 底层机制:只要当前类出现在实例的原型链上,结果都是true
* + 由于我们可以肆意的修改原型的指向,所以检测出来的结果是不准的
* + 不能检测基本数据类型
*/
function fun () {
this.name = 'fn'
}
fun.prototype = Object.create(Array.prototype)
let f = new fun()
console.log(f instanceof Array)
console.log(f instanceof Function)
// TODO:toString.call() 检测
const ruleType = data => Object.prototype.toString.call(data).slice(8, -1).toLowerCase()
console.log(ruleType(f))