一、原型鏈式
1. 基本思想:利用原型鏈實現繼承
2. 實現
plane.prototype.
fly=
function()
function
plane
(name)
var oplane1 =
newplane()
;var oplane2 =
newplane()
;//共有屬性,私有屬性
attackplane.prototype =
newplane()
;function
attackplane
(name)
var attcakplane1 =
newattackplane()
;var attcakplane2 =
newattackplane
('攻擊機');
<
/script>
3. 缺點:
1)引用值共享,改變乙個,兩個屬性都變
2)不能傳參
4.優點
1)可以繼承原型鏈上的屬性
2)構造器不會變
二、建構函式
2. 實現
plane.prototype.
fly=
function()
function
plane
(name)
function
attackplane
(name)
var attcakplane1 =
newattackplane(''
);var attcakplane2 =
newattackplane
('攻擊機');
<
/script>
3.缺點
1)原型鏈上的屬性和方法不能繼承
4.優點
1)可以傳參
2)引用值不會共享
三、組合繼承
1.基本思想:利用構造繼承和原型鏈組合
2.具體實現
plane.prototype.
fly=
function()
function
plane
(name)
attackplane.prototype =
newplane()
;function
attackplane
(name)
var attcakplane1 =
newattackplane
('攻擊機');
var attcakplane2 =
newattackplane(''
);attackplane.prototype.constructor = attackplane;
//必須加上
<
/script>
3.缺點
1)構造器會改變
2)會執行兩變函式
4.優點
1)可以繼承原型鏈上的屬性和方法
2)可以傳參
3)引用值不會共享
原型式繼承
1.基本思想:採用原型式繼承並不需要定義乙個類,傳入引數obj,生成乙個繼承obj物件的物件
2.具體實現
var person =
function
createobj
(obj)
f.prototype = obj;
return
newf()
;}var person1 =
createobj
(person)
;var person2 =
createobj
(person)
;<
/script>
3.缺點
1) 引用值共享
2) 構造器改變
3)不能傳參
4.優點
1)直接通過物件生成乙個繼承該物件的物件
五、寄生式繼承
1.基本思想:建立乙個僅僅用於封裝繼承過程的函式,然後在內部以某種方式增強物件,最後返回物件
2.具體實現
//建立乙個函式,函式通過
vartemp
=function()
; temp.prototype = plane.prototype; attackplane.prototype =
newtemp()
;//對物件的更改
//最後return new object
3.優點
1)引用值可以共享
六、寄生組合式繼承
1.基本思想:結合寄生式繼承和組合式繼承
2.具體實現
plane.prototype.
fly=
function()
function
plane
(name)
vartemp
=function()
; temp.prototype = plane.prototype;
attackplane.prototype =
newtemp()
; attackplane.prototype.
dan=
function()
attackplane.prototype.constructor = attackplane;
function
attackplane
(name)
var attackplane1 =
newattackplane(''
);var attackplane2 =
newattackplane
('攻擊機');
<
/script>
3.優點
1)引用值不共享
2)可以繼承原型鏈上屬性
JS的繼承方式
js繼承有5種實現方式 1 繼承第一種方式 物件冒充 function parent username function child username,password var parent new parent zhangsan var child new child lisi 123456 pa...
44 繼承的方式
當乙個類從另乙個類繼承時,子類能夠從父類繼承到的是父類的公有型別的成員屬性和成員方法,以及保護型別的成員屬性和成員方法。這些子類從父類繼承得到的屬性和方法,在子類中是什麼型別?這個問題與派生類的定義有關,基類的說明前有public protected和private進行說明,將從父類繼承來的屬性和方...
授權的方式繼承
classfilehandle def init self filename,mode r encoding utf 8 傳入的引數 self file open filename,mode,encoding encoding 獲得檔案控制代碼 也獲得了類所有方法 self mode mode se...