js 繼承方式

來源:互聯網
上載者:User

標籤:origin   方式   script   user   關係   繼承方式   use   art   nbsp   

前言:大多OO語言都支援兩種繼承方式: 介面繼承和實現繼承 ,而ECMAScript中無法實現介面繼承,ECMAScript只支援實現繼承,而且其實現繼承主要是依靠 原型鏈 來實現。1.原型鏈基本思想:利用原型讓一個參考型別繼承另外一個參考型別的屬性和方法。建構函式,原型,執行個體之間的關係:每個建構函式都有一個原型對象,原型對象包含一個指向建構函式的指標,而執行個體都包含一個指向原型對象的內部指標。原型鏈實現繼承例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function SuperType() {
this.property = true;
}
SuperType.prototype.getSuperValue = function() {
return this.property;
}
function subType() {
this.property = false;
}
//繼承了SuperType
SubType.prototype = new SuperType();
SubType.prototype.getSubValue = function (){
return this.property;
}
var instance = new SubType();
console.log(instance.getSuperValue());//true
2.借用建構函式基本思想:在子類型建構函式的內部調用超類建構函式,通過使用call()和apply()方法可以在新建立的對象上執行建構函式。例子:
1
2
3
4
5
6
7
8
9
10
11
function SuperType() {
this.colors = ["red","blue","green"];
}
function SubType() {
SuperType.call(this);//繼承了SuperType
}
var instance1 = new SubType();
instance1.colors.push("black");
console.log(instance1.colors);//"red","blue","green","black"
var instance2 = new SubType();
console.log(instance2.colors);//"red","blue","green"
3.組合繼承基本思想:將原型鏈和借用建構函式的技術組合在一塊,從而發揮兩者之長的一種繼承模式。例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function SuperType(name) {
this.name = name;
this.colors = ["red","blue","green"];
}
SuperType.prototype.sayName = function() {
console.log(this.name);
}
function SubType(name, age) {
SuperType.call(this,name);//繼承屬性
this.age = age;
}
//繼承方法
SubType.prototype = new SuperType();
Subtype.prototype.constructor = Subtype;
Subtype.prototype.sayAge = function() {
console.log(this.age);
}
var instance1 = new SubType("EvanChen",18);
instance1.colors.push("black");
consol.log(instance1.colors);//"red","blue","green","black"
instance1.sayName();//"EvanChen"
instance1.sayAge();//18
var instance2 = new SubType("EvanChen666",20);
console.log(instance2.colors);//"red","blue","green"
instance2.sayName();//"EvanChen666"
instance2.sayAge();//20
4.原型式繼承基本想法:藉助原型可以基於已有的對象建立新對象,同時還不必須因此建立自訂的類型。原型式繼承的思想可用以下函數來說明:
1
2
3
4
5
function object(o) {
function F(){}
F.prototype = o;
return new F();
}
例子:
1
2
3
4
5
6
7
8
9
10
11
var person = {
name:"EvanChen",
friends:["Shelby","Court","Van"];
};
var anotherPerson = object(person);
anotherPerson.name = "Greg";
anotherPerson.friends.push("Rob");
var yetAnotherPerson = object(person);
yetAnotherPerson.name = "Linda";
yetAnotherPerson.friends.push("Barbie");
console.log(person.friends);//"Shelby","Court","Van","Rob","Barbie"
ECMAScript5通過新增Object.create()方法正常化了原型式繼承,這個方法接收兩個參數:一個用作新對象原型的對象和一個作為新對象定義額外屬性的對象。
1
2
3
4
5
6
7
8
9
10
11
var person = {
name:"EvanChen",
friends:["Shelby","Court","Van"];
};
var anotherPerson = Object.create(person);
anotherPerson.name = "Greg";
anotherPerson.friends.push("Rob");
var yetAnotherPerson = Object.create(person);
yetAnotherPerson.name = "Linda";
yetAnotherPerson.friends.push("Barbie");
console.log(person.friends);//"Shelby","Court","Van","Rob","Barbie"
5.寄生式繼承基本思想:建立一個僅用於封裝繼承過程的函數,該函數在內部以某種方式來增強對象,最後再像真正是它做了所有工作一樣返回對象。例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
function createAnother(original) {
var clone = object(original);
clone.sayHi = function () {
alert("hi");
};
return clone;
}
var person = {
name:"EvanChen",
friends:["Shelby","Court","Van"];
};
var anotherPerson = createAnother(person);
anotherPerson.sayHi();///"hi"
6.寄生組合式繼承基本思想:通過借用函數來繼承屬性,通過原型鏈的混成形式來繼承方法其基本模型如下所示:
1
2
3
4
5
function inheritProperty(subType, superType) {
var prototype = object(superType.prototype);//建立對象
prototype.constructor = subType;//增強對象
subType.prototype = prototype;//指定對象
}
例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function SuperType(name){
this.name = name;
this.colors = ["red","blue","green"];
}
SuperType.prototype.sayName = function (){
alert(this.name);
};
function SubType(name,age){
SuperType.call(this,name);
this.age = age;
}
inheritProperty(SubType,SuperType);
SubType.prototype.sayAge = function() {
alert(this.age);
}
以上內容給大家介紹了javascript實現繼承的六種方式,希望對大家有所協助! 本文執行個體講述了js實現繼承的5種方式。分享給大家供大家參考,具體如下:1、繼承第一種方式:對象冒充
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function Parent(username){
this.username = username;
this.hello = function(){
alert(this.username);
}
}
function Child(username,password){
//通過以下3行實現將Parent的屬性和方法追加到Child中,從而實現繼承
//第一步:this.method是作為一個臨時的屬性,並且指向Parent所指向的對象,
//第二步:執行this.method方法,即執行Parent所指向的對象函數
//第三步:銷毀this.method屬性,即此時Child就已經擁有了Parent的所有屬性和方法
this.method = Parent;
this.method(username);//最關鍵的一行
delete this.method;
this.password = password;
this.world = function(){
alert(this.password);
}
}
var parent = new Parent("zhangsan");
var child = new Child("lisi","123456");
parent.hello();
child.hello();
child.world();
2、繼承第二種方式:call()方法方式call方法是Function類中的方法 call方法的第一個參數的值賦值給類(即方法)中出現的this call方法的第二個參數開始依次賦值給類(即方法)所接受的參數
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function test(str){
alert(this.name + " " + str);
}
var object = new Object();
object.name = "zhangsan";
test.call(object,"langsin");//此時,第一個參數值object傳遞給了test類(即方法)中出現的this,而第二個參數"langsin"則賦值給了test類(即方法)的str
function Parent(username){
this.username = username;
this.hello = function(){
alert(this.username);
}
}
function Child(username,password){
Parent.call(this,username);
this.password = password;
this.world = function(){
alert(this.password);
}
}
var parent = new Parent("zhangsan");
var child = new Child("lisi","123456");
parent.hello();
child.hello();
child.world();
3、繼承的第三種方式:apply()方法方式apply方法接受2個參數, A、第一個參數與call方法的第一個參數一樣,即賦值給類(即方法)中出現的this B、第二個參數為數群組類型,這個數組中的每個元素依次賦值給類(即方法)所接受的參數
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function Parent(username){
this.username = username;
this.hello = function(){
alert(this.username);
}
}
function Child(username,password){
Parent.apply(this,new Array(username));
this.password = password;
this.world = function(){
alert(this.password);
}
}
var parent = new Parent("zhangsan");
var child = new Child("lisi","123456");
parent.hello();
child.hello();
child.world();
4、繼承的第四種方式:原型鏈方式,即子類通過prototype將所有在父類中通過prototype追加的屬性和方法都追加到Child,從而實現了繼承
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function Person(){
}
Person.prototype.hello = "hello";
Person.prototype.sayHello = function(){
alert(this.hello);
}
function Child(){
}
Child.prototype = new Person();//這行的作用是:將Parent中將所有通過prototype追加的屬性和方法都追加到Child,從而實現了繼承
Child.prototype.world = "world";
Child.prototype.sayWorld = function(){
alert(this.world);
}
var c = new Child();
c.sayHello();
c.sayWorld();
5、繼承的第五種方式:混合方式混合了call方式、原型鏈方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function Parent(hello){
this.hello = hello;
}
Parent.prototype.sayHello = function(){
alert(this.hello);
}
function Child(hello,world){
Parent.call(this,hello);//將父類的屬性繼承過來
this.world = world;//新增一些屬性
}
Child.prototype = new Parent();//將父類的方法繼承過來
Child.prototype.sayWorld = function(){//新增一些方法
alert(this.world);
}
var c = new Child("zhangsan","lisi");
c.sayHello();
c.sayWorld();
希望本文所述對大家JavaScript程式設計有所協助。

js 繼承方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.