js原型prototype(实例构造函数的属性) __proto__(实例对象的属性) constructor(实例构造函数prototyper的属性)

 function Person(name,age){
    this.name=name
    this.age=age
   }
   Person.prototype.sayHi=function(){//原型是公共方法解决构造函数new对象公共属性和方法的内存浪费
    console.log(this.name+' say hi!!')
   }
   const p1=new Person('aa',12)
   const p2=new Person('bb',14)
   p1.sayHi()
   p2.sayHi()
   console.log(p1.__proto__===Person.prototype) //true
   console.log(p2.__proto__===Person.prototype)//true
   console.log(p1.__proto__===p2.__proto__)//true
   console.log(Person.prototype.constructor===Person)//true
  
console.log(Person.prototype.__proto__===Object.prototype) //true

 

posted @ 2023-05-27 10:56  howhy  阅读(7)  评论(0编辑  收藏  举报