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
面试题:
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 返回的是一个字符串,字符串说明数据的类型,number boolean string function object(null,数组,对象) undefined
instanceof 是用来判断A是否为B的实例对象,检测的是原型,基于原型链的检测,返回的是true/false
最准确检测类型: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]