一张图让你彻底理解js原型链

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_36774307/article/details/79729159
function Person() {
    this.name = 'sanlyshi';
    this.age = '23';
    this.eat = function () {
        console.log(this.name +' is eating!')
    }
}
Person.prototype.smell = function () {
    console.log("smell");
};
var person1 = new Person();
console.log(person1.name);   //sanlyshi
person1.eat();               //sanlyshi is eating!
person1.smell();             //smell


// 每个"函数"都有一个prototype属性,指向一个"对象"
// "对象"具有属性__proto__,可称为隐式原型,一个对象的隐式原型指向构造该对象的构造函数的原型
// 1
console.log(Object.prototype.__proto__ === null);   //true
// 2
console.log(Function.prototype.__proto__ === Object.prototype);   //true
// 3
console.log(Person.__proto__ === Function.prototype);   //true
// 4
console.log(person1.__proto__ === Person.prototype);   //true



// "prototype对象"有一个constructor属性,默认指向prototype对象所在的构造函数
console.log(Person.prototype.constructor === Person);   //true
console.log(Function.prototype.constructor === Function);   //true
console.log(Object.prototype.constructor === Object);   //true

猜你喜欢

转载自blog.csdn.net/weixin_36774307/article/details/79729159