1.instanceof 运算符的作用:
看下构造函数(Person)的prototype的值在不在对象p的原型链上,如果在就返回true,不在就返回false!!
语法:对象p instanceof 构造函数Person
例: function Person( ) {}
var p =new Person();
//p--->Person.prototype--->Object.prototype--->null //对象的prototype属性在对象p的原型链上
console.log(p instanceof Person) //true
可以还有一种情况。如果这个时候改变了Person的prototype
Person.protyope={}
console.log(p instanceof Person) //false //改变了对象的prototype属性就不在对象p的原型链上
总结: 对象在创建的那一刻原型链就定下来了
扩展:// 任何一个对象instanceof Object 都是 true
// 以下都是 true
console.log(new Object() instanceof Object);
console.log(new Array() instanceof Object);
console.log(new RegExp() instanceof Object);
console.log(new String() instanceof Object);
console.log(new Number() instanceof Object);
console.log(new Boolean() instanceof Object);
console.log(new Date() instanceof Object);
console.log(Math instanceof Object);
网友评论