构造函数,原型,实例之间的关系

2019-07-29  本文已影响0人  葵自渡_
关系:

1、每个构造函数都有一个原型属性prototype,它指向原型对象
2、原型对象都包含一个指向构造函数的指针(constructor)
3、而实例都包含一个指向原型对象的内置指针(__ proto__)

function Person(){             //构造函数
    }
    Person.prototype.name = "bree";      //在构造函数的原型对象上添加属性
    Person.prototype.isName = function(){
        alert(this.name)
    }
    var bree = new Person();        //调用构造函数创建的实例bree
    
    console.log(bree.__proto__ === Person.prototype);   //true
    console.log(Person.prototype.constructor === Person);  //true
上一篇 下一篇

猜你喜欢

热点阅读