2018.06.03javascript
funcion A(name) { this.name = name; // 實例基本屬性 (該屬性,強調私有,不共享) this.arr = [1]; // 實例引用屬性 (該屬性,強調私用,不共享) this.say = function() { // 實例引用屬性 (該屬性,強調複用,須要共享) console.log('hello') } } 注意:數組和方法都屬於‘實例引用屬性’,可是數組強調私有、不共享的。方法須要複用、共享。 注意:在構造函數中,通常不多有數組形式的引用屬性,大部分狀況都是:基本屬性 + 方法。
原型對象的用途是爲每一個實例對象存儲共享的方法和屬性,它僅僅是一個普通對象而已。而且全部的實例是共享同一個原型對象,所以有別於實例方法或屬性,原型對象僅有一份。而實例有不少份,且實例屬性和方法是獨立的。在構造函數中:爲了屬性(實例基本屬性)的私有性、以及方法(實例引用屬性)的複用、共享。咱們提倡:html
funcion A(name) { this.name = name; // (該屬性,強調私有,不共享) } A.prototype.say = function() { // 定義在原型對象上的方法 (強調複用,須要共享) console.log('hello') } // 不推薦的寫法:[緣由](https://blog.csdn.net/kkkkkxiaofei/article/details/46474303) A.prototype = { say: function() { console.log('hello') } }
優勢:方法複用java
缺點:數組
function Parent() { this.name = '父親'; // 實例基本屬性 (該屬性,強調私有,不共享) this.arr = [1]; // (該屬性,強調私有) } Parent.prototype.say = function() { // -- 將須要複用、共享的方法定義在父類原型上 console.log('hello') } function Child(like) { this.like = like; } Child.prototype = new Parent() // 核心 let boy1 = new Child() let boy2 = new Child() // 優勢:共享了父類構造函數的say方法 console.log(boy1.say(), boy2.say(), boy1.say === boy2.say); // hello , hello , true // 缺點1:不能傳參數 // 缺點2: console.log(boy1.name, boy2.name, boy1.name===boy2.name); // 父親,父親,true boy1.arr.push(2); // 修改了boy1的arr屬性,boy2的arr屬性,也會變化,由於兩個實例的原型上(Child.prototype)有了父類構造函數的實例屬性arr;因此只要修改了boy1.arr,boy2.arr的屬性也會變化。 ---- 原型上的arr屬性是共享的。 console.log(boy2.arr); // [1,2] 注意:修改boy1的name屬性,是不會影響到boy2.name。由於name是基本屬性,不是引用屬性。
優勢:實例之間獨立。app
缺點:函數
因爲方法在父構造函數中定義,致使方法不能複用(由於每次建立子類實例都要建立一遍方法)。好比say方法。(方法應該要複用、共享)
function Parent(name) { this.name = name; // 實例基本屬性 (該屬性,強調私有,不共享) this.arr = [1]; // (該屬性,強調私有) this.say = function() { // 實例引用屬性 (該屬性,強調複用,須要共享) console.log('hello') } } function Child(name,like) { Parent.call(this,name); // 核心 this.like = like; } let boy1 = new Child('小紅','apple'); let boy2 = new Child('小明', 'orange '); // 優勢1:可傳參 console.log(boy1.name, boy2.name); // 小紅, 小明 // 優勢2:不共享父類構造函數的引用屬性 boy1.arr.push(2); console.log(boy1.arr,boy2.arr);// [1,2] [1] // 缺點1:方法不能複用 console.log(boy1.say === boy2.say) // false (說明,boy1和boy2 的say方法是獨立,不是共享的) // 缺點2:不能繼承父類原型上的方法 Parent.prototype.walk = function () { // 在父類的原型對象上定義一個walk方法。 console.log('我會走路') } boy1.walk; // undefined (說明實例,不能得到父類原型上的方法)
優勢:優化
缺點:this
第一次Parent.call(this);從父類拷貝一份父類實例屬性,做爲子類的實例屬性,第二次Child.prototype = new Parent();建立父類實例做爲子類原型,此時這個父類實例就又有了一份實例屬性,但這份會被第一次拷貝來的實例屬性屏蔽掉,因此多餘。.net
爲啥是兩次?若是仍是,不清楚,能夠看文末,我會詳細講解!prototype
function Parent(name) { this.name = name; // 實例基本屬性 (該屬性,強調私有,不共享) this.arr = [1]; // (該屬性,強調私有) } Parent.prototype.say = function() { // --- 將須要複用、共享的方法定義在父類原型上 console.log('hello') } function Child(name,like) { Parent.call(this,name,like) // 核心 第二次 this.like = like; } Child.prototype = new Parent() // 核心 第一次 <!--這裏是修復構造函數指向的代碼--> let boy1 = new Child('小紅','apple') let boy2 = new Child('小明','orange') // 優勢1:能夠傳參數 console.log(boy1.name,boy1.like); // 小紅,apple // 優勢2:可複用父類原型上的方法 console.log(boy1.say === boy2.say) // true // 優勢3:不共享父類的引用屬性,如arr屬性 boy1.arr.push(2) console.log(boy1.arr,boy2.arr); // [1,2] [1] 能夠看出沒有共享arr屬性。 注意:爲啥要修復構造函數的指向? console.log(boy1.constructor); // Parent 你會發現實例的構造函數竟然是Parent。 而實際上,咱們但願子類實例的構造函數是Child,因此要記得修復構造函數指向。修復以下 Child.prototype.constructor = Child;
其實Child.prototype = new Parent()console.log(Child.prototype.__proto__ === Parten.prototype); // true
經過這種方式,砍掉父類的實例屬性,這樣在調用父類的構造函數的時候,就不會初始化兩次實例,避免組合繼承的缺點。
優勢:
缺點:
緣由是:不能判斷子類實例的直接構造函數,究竟是子類構造函數仍是父類構造函數。
function Parent(name) { this.name = name; // 實例基本屬性 (該屬性,強調私有,不共享) this.arr = [1]; // (該屬性,強調私有) } Parent.prototype.say = function() { // --- 將須要複用、共享的方法定義在父類原型上 console.log('hello') } function Child(name,like) { Parent.call(this,name,like) // 核心 this.like = like; } Child.prototype = Parent.prototype // 核心 子類原型和父類原型,實質上是同一個 <!--這裏是修復構造函數指向的代碼--> let boy1 = new Child('小紅','apple') let boy2 = new Child('小明','orange') let p1 = new Parent('小爸爸') // 優勢1:能夠傳參數 console.log(boy1.name,boy1.like); // 小紅,apple // 優勢2: console.log(boy1.say === boy2.say) // true // 缺點1:當修復子類構造函數的指向後,父類實例的構造函數指向也會跟着變了。 具體緣由:由於是經過原型來實現繼承的,Child.prototype的上面是沒有constructor屬性的,就會往上找,這樣就找到了Parent.prototype上面的constructor屬性;當你修改了子類實例的construtor屬性,全部的constructor的指向都會發生變化。 沒修復以前:console.log(boy1.constructor); // Parent 修復代碼:Child.prototype.constructor = Child 修復以後:console.log(boy1.constructor); // Child console.log(p1.constructor);// Child 這裏就是存在的問題(咱們但願是Parent)
function Parent(name) { this.name = name; // 實例基本屬性 (該屬性,強調私有,不共享) this.arr = [1]; // (該屬性,強調私有) } Parent.prototype.say = function() { // --- 將須要複用、共享的方法定義在父類原型上 console.log('hello') } function Child(name,like) { Parent.call(this,name,like) // 核心 this.like = like; } Child.prototype = Object.create(Parent.prototype) // 核心 經過建立中間對象,子類原型和父類原型,就會隔離開。不是同一個啦,有效避免了方式4的缺點。 <!--這裏是修復構造函數指向的代碼--> Child.prototype.constructor = Child let boy1 = new Child('小紅','apple') let boy2 = new Child('小明','orange') let p1 = new Parent('小爸爸') 注意:這種方法也要修復構造函數的 修復代碼:Child.prototype.constructor = Child 修復以後:console.log(boy1.constructor); // Child console.log(p1.constructor);// Parent 完美
Object.create() 的第二參數,是可選的。
- Object.create() 的內部原理: // 其中,o 是新建立對象的原型(對象) function object(o) { function F() {} F.prototype = o return new F() } 注意:以前,Object.create()沒有出現以前,就是採用的這種方式。 參見《js高級程序設計》P170
如下是個人我的看法,(若有不對,還請指正):new 產生的實例,優先獲取構造函數上的屬性;構造函數上沒有對應的屬性,纔會去原型上查找;若是構造函數中以及原型中都沒有對應的屬性,就會報錯。
Object.create() 產生的對象,只會在原型上進行查找屬性,原型上沒有對應的屬性,就會報錯。
let Base1 = function() { this.a = 1 } let o1 = new Base1() let o2 = Object.create(Base1.prototype) console.log(o1.a); // 1 console.log(o2.a); // undefined let Base2 = function() {} Base2.prototype.a = 'aa' let o3 = new Base2() let o4 = Object.create(Base2.prototype) console.log(o3.a); // aa console.log(o4.a); // aa let Base3 = function() { this.a = 1 } Base3.prototype.a = 'aa' let o5 = new Base3() let o6 = Object.create(Base3.prototype) console.log(o5.a); // 1 console.log(o6.a); // aa
funciton Func(name) { this.name = name } let p = new Func('小紅')
new 的過程,作了啥?作了四件事。
obj.__proto__ = Func.prototype 就是:將新對象的__proto__ 指向構造函數的prototype
let result = Func.call(obj) 就是:使用call或apply,將構造函數的this綁定到新對象,並執行構造函數
若是是引用類型,就返回這個引用類型的對象。若是是值類型或沒有return,則返回空對象obj。 if (typeof(result) === "object"){ func=result; } else{ func=obj; // 默認返回 } 注意:js中的構造函數,是不須要有返回值的,因此默認返回的是新建立的空對象obj
‘new 的過程’的第三步,其實就是執行了父類構造函數。
call的做用是改變函數執行時的上下文。好比:A.call(B)。其實,最終執行的仍是A函數,只不過是用B來調用而已。因此,你就懂了Parent.call(this,name,like) ,也就是執行了父類構造函數。