function Student(name){
this.name = name;
this.say = function (){
console.log(’my Name is ’, this.name)
}
}
Student.prototype.eat= function eat(){
console.log(this.name + ‘is eating’)
}
let s1= new Student(“Tom”)
let s2= new Student(“Lucy”)
s1 s2 Student之间的关系见下图

可以推导出如下的关系:
s1.__proto__ === s2.__proto__ ===Student.prototype
s1.constructor === s2.constructor === Student.prototype.constructor === Student
Student.prototype.__proto__ === Object.prototype
Object.__proto__ ===null
任何一个函数的prototype都有2个属性:
constructor
和__proto__

任何一个函数的prototype的constructor属性都指向自身
People.prototype.constructor === People // true
如果一个对象是由类实例化而来,那么它的
__proto__
指向该类的原型
如果一个对象不是由类实例化而来,那么它的
__proto__
指向Object.prototype
比如
let obj = {} // 手动新建一个对象
obj.__proto__ === Object.prototype // true
因为 函数的
prototype
是一个对象,该对象不是由类实例化而来,根据上一条,其__proto__
指向Object.prototype
Student.prototype.__proto__ === Object.prototype
网友评论