对象的几个方法

 1 var obj = {
 2     name: "heihei",
 3     age: 18
 4 }
 5 var info = Object.create(obj, {
 6     address: {
 7         value: "成都",
 8         enumerable: true
 9     }
10 })
11 
12 function Person() {
13 
14 }
15 var p = new Person()
16 console.log(info.name);
17 //1判断是否自己身上的属性
18 console.log(info.hasOwnProperty('name'));
19 console.log(info.hasOwnProperty('address'));
20 //2:判断info是否在obj的原型链上
21 // console.log(info instanceof obj) 会报错 只能判断是不是在构造函数的原型链
22 console.log(obj.isPrototypeOf(info));
23 // 3:instanceof 和isPrototypeof类似的功能但是有限制
24 console.log(p instanceof Person);
25 console.log(Person.prototype.isPrototypeOf(p));

 

 

主要注意instanceof 和isPrototypeOf的区别

posted @ 2022-03-20 19:23  沁霓  阅读(39)  评论(0)    收藏  举报