構造函數,原型,實例之間的關係

關係:


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  
  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章