日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

JavaScript学习13 JavaScript中的继承

發(fā)布時間:2025/3/15 javascript 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JavaScript学习13 JavaScript中的继承 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?

JavaScript學習13 JavaScript中的繼承

?

繼承第一種方式:對象冒充

<script type="text/javascript">//繼承第一種方式:對象冒充function Parent(username) //父類對象 {this.username = username; //下面的代碼最關(guān)鍵的部分就是將子對象的this傳遞給了父對象this.sayHello = function(){alert(this.username);} }function Child(username, password) //子類對象 {//下面三行代碼是最關(guān)鍵的代碼this.method = Parent; //定義method屬性,指向Parent,即指向了上面的構(gòu)造函數(shù)this.method(username);//把username傳遞過去,調(diào)用構(gòu)造函數(shù),此時Parent函數(shù)體中的this即當前的Child對象delete this.method; //刪掉method屬性,因為Child已經(jīng)具備了Parent的屬性和方法//下面可以增加一些子類特有的屬性和方法this.password = password;this.sayWorld = function(){alert(this.password);} }//生成這兩個類的對象 var parent = new Parent("zhangsan"); var child = new Child("lisi", "1234");parent.sayHello();child.sayHello(); child.sayWorld();</script>

  

  使用這種方式實現(xiàn)繼承的時候,JS可以實現(xiàn)多重的繼承,但是有時候會造成一些干擾,比如同名方法的覆蓋,所以不太推薦使用多繼承。

?

繼承第二種方式:call方法方式

  call方法是定義在Function對象中的方法,因此我們定義的每個函數(shù)都有該方法。

  可以通過函數(shù)名來調(diào)用call方法,call方法的第一個參數(shù)會被傳遞給函數(shù)中的this,從第二個參數(shù)開始,逐一賦值給函數(shù)中的參數(shù)

  call方法

<script type="text/javascript">//call方法方式,Function對象中的方法function test(str, str2) {alert(this.name + ", " + str + ", " + str2); }var object = new Object(); object.name = "zhangsan";//test.call相當于調(diào)用了test函數(shù) test.call(object, "shengsiyuan", "hello"); //將object賦給了this //第一個參數(shù)賦給this,后面的參數(shù)逐次賦給方法參數(shù)</script>

?

  call方法實現(xiàn)繼承

<script type="text/javascript">//使用call方式實現(xiàn)對象的繼承function Parent(username) {this.username = username;this.sayHello = function(){alert(this.username);} }function Child(username, password) {Parent.call(this, username);//這樣語句很關(guān)鍵,等價于對象冒充中的三行語句//執(zhí)行之后子類就具有了基類的屬性和方法//子對象的新增屬性和方法this.password = password;this.sayWorld = function(){alert(this.password);} }var parent = new Parent("zhangsan");var child = new Child("lisi", "123");parent.sayHello();child.sayHello(); child.sayWorld();</script>

?

繼承第三種方式:apply方法方式

  apply和call一樣,都是定義在Function對象里面的。

<script type="text/javascript">//使用apply方法實現(xiàn)對象繼承function Parent(username) {this.username = username;this.sayHello = function(){alert(this.username);} }function Child(username, password) {Parent.apply(this, new Array(username));//apply方法方式實現(xiàn)繼承,后面的參數(shù)以數(shù)組的形式傳入this.password = password;this.sayWorld = function(){alert(this.password);} }var parent = new Parent("zhangsan"); var child = new Child("lisi", "123");parent.sayHello();child.sayHello(); child.sayWorld();</script>

?

繼承第四種方式:原型鏈方式

<script type="text/javascript">//使用原型鏈(prototype chain)方式實現(xiàn)對象繼承function Parent() {}Parent.prototype.hello = "hello"; Parent.prototype.sayHello = function() {alert(this.hello); }function Child() {}Child.prototype = new Parent();//子類具有父類的屬性和方法 //擴展屬性 Child.prototype.world = "world"; Child.prototype.sayWorld = function() {alert(this.world); }var child = new Child();child.sayHello(); child.sayWorld();</script>

  原型鏈的方式,缺點是無法給構(gòu)造函數(shù)傳遞參數(shù)。

?

繼承的第五種方式:混合方式

  這種方式是對原型鏈方式的一種改進,使得可以向構(gòu)造函數(shù)傳遞參數(shù)。

  這種方式是比較推薦的一種方式。

<script type="text/javascript">//使用混合方式實現(xiàn)對象繼承(推薦) //父對象 function Parent(hello) {this.hello = hello; }Parent.prototype.sayHello = function() {alert(this.hello); } //子對象 function Child(hello, world) {Parent.call(this, hello);//通過call實現(xiàn)屬性的繼承this.world = world;//新增加的屬性 }Child.prototype = new Parent();//通過原型鏈實現(xiàn)方法的繼承 Child.prototype.sayWorld = function() {alert(this.world); }var child = new Child("hello", "world");child.sayHello(); child.sayWorld();</script>

?

實例

<script type="text/javascript">function Shape(edge) {this.edge = edge;//屬性,多少條邊,通過構(gòu)造函數(shù)的方式定義 }Shape.prototype.getArea = function()//通過原型的方式來定義方法 {return -1;//基類返回一個沒有意義的值 }Shape.prototype.getEdge = function() {return this.edge; }//定義子對象Triangle function Triangle(bottom, height) {Shape.call(this, 3);//Triangle的屬性this.bottom = bottom;this.height = height; }Triangle.prototype = new Shape();//繼承Shaple中的所有方法 Triangle.prototype.getArea = function()//重寫方法 {return 0.5 * this.bottom * this.height; }var triangle = new Triangle(10, 4);//alert(triangle.getEdge()); //alert(triangle.getArea());//另一個子類:四邊形 function Rectangle(bottom, height) {Shape.call(this, 4);this.bottom = bottom;this.height = height; }Rectangle.prototype = new Shape();//通過原型繼承父類的所有方法 Rectangle.prototype.getArea = function()//覆寫方法 {return this.bottom * this.height; }var rectangle = new Rectangle(20, 40);alert(rectangle.getEdge()); alert(rectangle.getArea());</script>

?

參考資料

  圣思園張龍老師Java Web系列視頻教程。

轉(zhuǎn)載于:https://www.cnblogs.com/mengdd/p/3767858.html

總結(jié)

以上是生活随笔為你收集整理的JavaScript学习13 JavaScript中的继承的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。