久久久久在线观看_又色又爽又黄的免费视频播放_一区中文字幕_日韩电影在线播放

今日頭條 焦點資訊 營銷之道 企業報道 淘寶運營 網站建設 軟件開發 400電話
  當前位置: 首頁 » 資訊 » 軟件開發 » 正文

5種js實現繼承的方式實例分享

放大字體  縮小字體 發布日期:2018-02-27  來源:企業800網  作者:新格網  瀏覽次數:869  【去百度看看】
核心提示:本文主要和大家分享5種js實現繼承的方式實例分享,希望能幫助大家更好掌握js繼承方式的實現。
本文主要和大家分享5種js實現繼承的方式實例分享,希望能幫助大家更好掌握js繼承方式的實現。

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程序設計有所幫助。

相關推薦:

學習javascript面向對象 javascript實現繼承的方式_javascript技巧

js面向對象之繼承知識詳解

Javascript實現多重繼承實例詳解

以上就是5種js實現繼承的方式實例分享的詳細內容,更多請關注php中文網其它相關文章!

 
 
[ 資訊搜索 ]  [ 加入收藏 ]  [ 告訴好友 ]  [ 打印本文 ]  [ 違規舉報 ]  [ 關閉窗口 ]

 
0條 [查看全部]  相關評論

 
網站首頁 | 關于我們 | 聯系方式 | 使用協議 | 版權隱私 | 網站地圖 | 排名推廣 | 廣告服務 | 積分換禮 | 網站留言 | RSS訂閱 | 吉ICP備11001726號-6
企業800網 · 提供技術支持