typeof
var str='laoliang'//使用if(typeof str === 'string'){}typeof ''; // string 有效typeof 1; // number 有效typeof Symbol(); // symbol 有效typeof true; //boolean 有效typeof undefined; //undefined 有效typeof null; //object 无效typeof [] ; //object 无效typeof new Function(); // function 有效typeof new Date(); //object 无效typeof new RegExp(); //object 无效
总结:
- 对于基本类型,除了null外,其他都可以返回正确的类型
- 对于引用类型,除了function,都是object类型
instanceof
instanceof判断A是否为B的实例,是则返回true,否则false:A instanceof B
```javascript //instanceof的实现 function newinstanceof(leftVaule, rightVaule) { let rightProto = rightVaule.prototype; // 取右表达式的 prototype 值 let leftVaule = leftVaule.proto; // 取左表达式的__proto值 while (true) { // 无限循环,在原型链上查找,知道满足条件return 跳出循环 if (leftVaule === null) { // 原型链的顶端var arr=[]arr instanceof Array // true
} if (leftVaule === rightProto) {return false;
} leftVaule = leftVaule.proto } }return true;
var arr = [] let d = new_instance_of(arr, Array) console.log(d)
A instanceof B, 在A的原型链中层层查找,是否有原型等于B.prototype,如果一直找到A的原型链的顶端(null;即Object.prototype.__proto__),仍然不等于B.prototype,那么返回false,否则返回true.<br />总结:- instanceof只能判断两个对象是否为实例关系,而不能判断一个对象实例具体属于哪个类型```javascript[] instanceof Array; // true{} instanceof Object;// truenew Date() instanceof Date;// truefunction Person(){};new Person() instanceof Person;[] instanceof Object; // truenew Date() instanceof Object;// truenew Person instanceof Object;// true
constructor
当一个函数 F被定义时,JS引擎会为F添加 prototype 原型,然后再在 prototype上添加一个 constructor 属性,并让其指向 F 的引用
当执行 var f = new F() 时,F 被当成了构造函数,f 是F的实例对象,此时 F 原型上的 constructor 传递到了 f 上,因此 f.constructor == F
从原型链角度讲,构造函数 F 就是新对象的类型。这样做的意义是,让新对象在诞生以后,就具有可追溯的数据类型
总结
- null 和 undefined 是无效的对象,因此是不会有 constructor 存在的,这两种类型的数据需要通过其他方式来判断。
- 函数的 constructor 是不稳定的,这个主要体现在自定义对象上,当开发者重写 prototype 后,原有的 constructor 引用会丢失,constructor 会默认为 Object

为什么变成了 Object?
因为 prototype 被重新赋值的是一个 { }, { } 是 new Object() 的字面量,因此 new Object() 会将 Object 原型上的 constructor 传递给 { },也就是 Object 本身。
因此,为了规范开发,在重写对象原型时一般都需要重新给 constructor 赋值,以保证对象实例的类型不被篡改。
var o={a:"MMM"}function F(){}F.prototype=ovar f =new F()F.prototype.constructor=Ff.constructor ===F//true
Object.prototype.toString
toString() 是 Object 的原型方法,调用该方法,默认返回当前对象的 [[Class]] 。这是一个内部属性,其格式为 [object Xxx] ,其中 Xxx 就是对象的类型。
对于 Object 对象,直接调用 toString() 就能返回 [object Object] 。而对于其他对象,则需要通过 call / apply 来调用才能返回正确的类型信息。
Object.prototype.toString.call('') ; // [object String]Object.prototype.toString.call(1) ; // [object Number]Object.prototype.toString.call(true) ; // [object Boolean]Object.prototype.toString.call(Symbol()); //[object Symbol]Object.prototype.toString.call(undefined) ; // [object Undefined]Object.prototype.toString.call(null) ; // [object Null]Object.prototype.toString.call(new Function()) ; // [object Function]Object.prototype.toString.call(new Date()) ; // [object Date]Object.prototype.toString.call([]) ; // [object Array]Object.prototype.toString.call(new RegExp()) ; // [object RegExp]Object.prototype.toString.call(new Error()) ; // [object Error]Object.prototype.toString.call(document) ; // [object HTMLDocument]Object.prototype.toString.call(window) ; //[object global] window 是全局对象 global 的引用Object.prototype.toString(Object) // '[object Object]'
