1 理解Javascript constructor實現原理javascript
在 JavaScript 中,每一個函數都有名爲「prototype」的屬性,用於引用原型對象。此原型對象又有名爲「constructor」的屬性,它反過來引用函數自己。這是一種循環引用html
2 JavaScript探祕:構造函數 Constructorjava
除了建立對象,構造函數(constructor) 還作了另外一件有用的事情—自動爲建立的新對象設置了原型對象(prototype object) 。原型對象存放於 ConstructorFunction.prototype 屬性中。segmentfault
3 JavaScript繼承方式詳解函數
1)原型鏈繼承this
function Parent(){ this.name = 'mike'; } function Child(){ this.age = 12; } Child.prototype = new Parent();//Child繼承Parent,經過原型,造成鏈條 var test = new Child(); alert(test.age); alert(test.name);//獲得被繼承的屬性 }
2)組合繼承.net
function Parent(age){ this.name = ['mike','jack','smith']; this.age = age; } Parent.prototype.run = function () { return this.name + ' are both' + this.age; }; function Child(age){ Parent.call(this,age);//對象冒充,給超類型傳參 } Child.prototype = new Parent();//原型鏈繼承 var test = new Child(21);//寫new Parent(21)也行 alert(test.run());//mike,jack,smith are both21
使用原型鏈實現對原型屬性和方法的繼承,而經過借用構造函數來實現對實例屬性的繼承。這樣,既經過在原型上定義方法實現了函數複用,又保證每一個實例都有它本身的屬性。prototype
4 JavaScript 繼承詳解htm
一個系列文章,沒有看完對象