Typeof and instanceof detect similarities and differences of data types

console.log(typeof(null)) //object  为什么不是null
//计算机typeof 返回的数据类型  机器码后三位是000 返回object 01011:000=>object
//null 0000.....000=>object
//js  bug

console.log(typeof Array) //function 为什么不是object
// typeof 引用类型 object: object function
// object 定义一个内部方法[[call]]: 有call方法返回function   无call返回object

Interview questions:

var str = 'jiajia';
console.log(str);//jiajia
console.log(typeof str);//string

var str1 = new String('佳佳');//实例化后的对象

console.log(str1);//{} 0:"佳" 1:"佳"
console.log(typeof str1);//object
console.log([] instanceof Object);//true
console.log(new Date() instanceof Object);//true
function Person(params) { }
console.log(new Person instanceof Object);//true
// instanceof 原型链   A  是不是B实例化过来的,如果找不到 继续向上寻找 C;  
// 如果A instanceof B 返回true,B instanceof C 返回true  ,那就可以说明A也是C实例化后的

typeof returns a string, the string specifies the type of data, number boolean string function object(null, array, object) undefined

instanceof is used to judge whether A is an instance object of B. It detects the prototype. Based on the detection of the prototype chain, it returns true/false

Most accurate type detection: Object.prototype.toString.call

console.log(Object.prototype.toString.call('1')); //[object String]
console.log(Object.prototype.toString.call([])); //[object Array]
console.log(Object.prototype.toString.call(null)); //[object Null]

Guess you like

Origin blog.csdn.net/qq_40269801/article/details/130384805