繼承的幾種方式

1.借助構造函數實現繼承

function Parent() {
this.name = 'parent'
}
Parent.prototype.say = function () { // 不能被繼承
this.say = function() {
console.log('hello'+ this.name)
}
}
function Child() {
Parent.call(this)
this.type = 'child'
}
console.log(new Child) // 沒有參數可以不寫最后的()

call方法改變了函數運行的上下文(this的指向,指向了Child實例化的對象引用),將父級構造函數的this指向子類構造函數的實例上去。執行時父類的方法和屬性都掛載到Child類的實例上

缺點:
父類的原型對象上有屬性和方法不能被子類繼承

?

2.借助原型鏈實現繼承

function Parent2() {
this.name = 'parent2'
this.list = [1,2,5]
}
Parent2.prototype.say = function () { // 不能被繼承
this.say = function() {
console.log('hello'+ this.name)
}
}
function Child2() {
this.type = 'child'
}
Child2.prototype = new Parent2()
var c21 = new Child2()
var c22 = new Child2()// 缺點
c21.list.push(6)
console.log(c22.list) // [1, 2, 5, 6]

prototype就是讓Child的實例可以訪問到原型鏈上

根據原型鏈
由 Child2.prototype = new Parent2()
和 c21.__proto__ === Child2.prototype
得 c21.__proto__ 指向了new Parent2()
所以 c21.__proto__.__proto__ 與 new Parent2().__proto__為同一個對象
又因為 new Parent2().__proto__ === Parent2.prototype
所以c21.__proto__.__proto__指向 Parent2.prototype

缺點:
原型鏈中的原型對象是公用的

?

3.組合繼承

function Parent3() {
this.name = 'parent3'
this.list = [1,2,5]
}
function Child3() {
Parent3.call(this)
this.type = 'child3'
}
Child3.prototype = new Parent3()
var c3 = new Child3()

缺點:
Parent3()執行了兩次,一次在Child3()里面,一次是給Child2.prototype賦值時

?

?

4. 組合繼承優化1

function Parent4() {
this.name = 'parent4'
this.list = [1,2,5]
}
function Child4() {
Parent5.call(this)
this.type = 'child4'
}
Child4.prototype = Parent4.prototype
var c4 = new Child4()// 缺點
console.log(c31.__proto__.constructor) // Parent4

缺點:
因為Child4.prototype = Parent4.prototype,所以Child4沒有構造器,是從父類繼承的

?

?

5. 組合繼承優化2

function Parent5() {
this.name = 'parent5'
this.list = [1,2,5]
}
function Child5() {
Parent5.call(this)
this.type = 'child5'
}
Child5.prototype = Object.create(Parent5.prototype)
Child5.prototype.constructor = Child5 
var c5 = new Child5()

缺點:
。。。可能就是比較麻煩吧

?

6.ES6實現繼承(與5的效果相同)

class Animal {constructor(name='animal') {this.name = name}eat () {console.log('I can eat')}
}class Dog extends Animal {constructor(name='dog') {super(name)}bark() {console.log('bark')}
}let dog1 = new Dog 
let dog2 = new Dog('dd')

?

轉載于:https://www.cnblogs.com/daisy-ramble/p/10331392.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/278385.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/278385.shtml
英文地址,請注明出處:http://en.pswp.cn/news/278385.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

寫一個簡單的 django_post demo

1.新建一個django工程,其路由為下圖 2.要做的是一個 簡單的登錄請求,以表單形式提交,html 部分代碼如下 這里注意action指向的是路由的地址,index1后的views.login部分代碼如下 這段代碼指的是,如果login接收到的請求是…

日志收集

2019獨角獸企業重金招聘Python工程師標準>>> ELK (ElasticSearch、Logstash、Kibana): https://my.oschina.net/itblog/blog/547250 轉載于:https://my.oschina.net/zfscofield/blog/1625703

autocopy2u_借助AutoCopy簡化Firefox中的文本復制和粘貼

autocopy2uLooking for an easy way to speed up copying and pasting in Firefox? Now you can reduce the amount of work that you have to do by half with AutoCopy. 是否在尋找一種簡便的方法來加快Firefox中的復制和粘貼? 現在,您可以使用自動復…

virtualenv模塊使用

開發多個應用: 如A需要jinja2.7開發;如B需要jinja2.6開發。或者C需要Python2.7開發,D需要Python3.5開發 那么解決上述問題就需要使用virtualenv這個模塊: 它的作用是:創建“隔離”環境,使項目擁有獨立的Pyt…

僵尸進程處理方式

Linux服務器上,多少會出現一些僵尸進程,下面介紹如何快速尋找和消滅這些僵尸進程的方法 首先,我們可以用top命令來查看服務器當前是否有僵尸進程,在下圖中可以看到僵尸進程數的提示,如果數字大于0,那么意味…

chromebook刷機_如何查看Chromebook的停產日期

chromebook刷機Google谷歌There comes a time in your Chromebook’s life when it no longer receives updates from Google. It’s inevitable and could be a lot sooner than you think. Here’s how to see your Chromebook’s scheduled end-of-life date. Chromebook一生…

C#將unix時間戳轉換成.net的DateTime類型的代碼

下面的內容是關于C#將unix時間戳轉換成.net的DateTime類型的內容。 DateTime epoch new DateTime(1970,1,1,0,0,0,0, DateTimeKind.Utc);DateTime myDate epoch.AddSeconds(1258598728).toLocalTime(); 轉載于:https://www.cnblogs.com/odsxe/p/10338494.html

【活動】AI人工智能技術沙龍 |杭州站

AI人工智能技術沙龍 |杭州站將于2018年3月3號在浙江杭州市文一西路1818-2號中國(杭州)人工智能小鎮舉辦由袋鼠云、七牛云及“因特鏈”社區的老師為大家帶來AI純技術干貨分享另有區塊鏈和AI人工智能技術融合技術主題1活動安排時間:2018年3月3號…

如何在Google文檔中的圖片周圍換行

If you want to insert an image or object into a document, it’s relatively simple. However, positioning and getting them to stay where you want can be frustrating. The wrap text feature in Google Docs makes all of this more manageable. 如果要將圖像或對象插…

mysql的left函數

1、LEFT()函數是一個字符串函數,它返回具有指定長度的字符串的左邊部分。 LEFT(Str,length); 接收兩個參數: str:一個字符串; length:想要截取的長度,是一個正整數; 2、示例: SELECT…

如何在Linux上使用history命令

Fatmawati Achmad Zaenuri/ShutterstockFatmawati Achmad Zaenuri / ShutterstockLinux’s shell saves a history of the commands you run, and you can search it to repeat commands you’ve run in the past. Once you understand the Linux history command and how to u…

mysql導入sqlserver數據庫表

原文:https://zhidao.baidu.com/question/1114325744502691499.html 在Navicat for MySQL 管理器中,創建目標數據庫(注意:因為是點對點的數據導入,要求sql server 中要導出的數據庫名稱和要導入到Mysql 中的數據庫的名字相同)點擊…

AppleScript

AppleScript 后綴名scpt do shell script "shell腳本"轉載于:https://www.cnblogs.com/yangwenhuan/p/10338580.html

ER圖三元聯系簡介

數據庫設計時,遇到三元聯系怎樣確定,下面做個簡單介紹。 一、確定聯系 三元聯系共 4 種情況: 1 : 1 : 11 : 1 : N1 : M : NM : N : P1 &#xff1…

mac自帶郵箱導出郵件_如何將電子郵件從Mac Mail導出到Notes應用程序

mac自帶郵箱導出郵件Khamosh PathakKhamosh PathakIf you use the Mail app regularly, you’re used to archiving or flagging emails for later. But what if you want to save a particular message for future reference in the Notes app? Well, there’s a work-around…

luogu P3380 【模板】二逼平衡樹(樹套樹)

恭喜你 以分塊的姿勢通過了此題 #include<cmath> #include<cstdio> #include<algorithm> #define inf (2147483647) using namespace std; const int N5e450; struct FK {int l,r;} K[500]; int n,m,T,sqr,ans,a[N],b[N],fa[N]; void work1(int l,int r,int …

Appium使用Python運行appium測試的實例

Appium使用Python運行appium測試的實例 一&#xff0e; Appium之介紹 https://testerhome.com/topics/8038 詳情參考-- https://testerhome.com/topics/8038 Appium是一個移動端的自動化框架&#xff0c;可用于測試原生應用&#xff0c;移動網頁應用和混合型應用&#xff0c;且…

ubuntu 任務欄監視器_從系統任務欄監視Google服務

ubuntu 任務欄監視器Are you looking for an app that sits in your System Tray and will notify you when you have new items in your Google accounts? Now you can easily monitor all of your favorite Google services with Googsystray. 您是否正在尋找一個位于系統任…

Java發送郵件(帶附件)

實現java發送郵件的過程大體有以下幾步&#xff1a; 準備一個properties文件&#xff0c;該文件中存放SMTP服務器地址等參數。利用properties創建一個Session對象利用Session創建Message對象&#xff0c;然后設置郵件主題和正文利用Transport對象發送郵件需要的jar有2個&#x…

google天氣預報接口_將天氣預報添加到谷歌瀏覽器

google天氣預報接口Are you looking for a quick and easy way to see your local weather forecast in Google Chrome? Then you will definitely want to take a good look at the AccuWeather Forecast extension. 您是否正在尋找一種快速簡便的方法來在Google Chrome瀏覽器…