js原型链总结示意图

2019-06-17  本文已影响0人  louhangfei
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之间的关系见下图

image.png

可以推导出如下的关系:

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

任何一个函数的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
上一篇 下一篇

猜你喜欢

热点阅读