原型鏈的圖示及代碼演示

1.class

constructor

屬性

方法


// 類

class Student {

? ? // 構造函數(shù)

? ? constructor(name, number) { // 屬性

? ? ? ? this.name = name

? ? ? ? this.number = number

? ? }

? ? // 方法

? ? sayHi() {

? ? ? ? console.log(`姓名 ${this.name} 學號 ${this.number}`)

? ? }

}

// 實例

const xialuo = new Student('夏洛', 100)

console.log(xialuo.name, xialuo.number)

xialuo.sayHi()

// 實例

const madongmei = new Student('馬冬梅', 101)

console.log(madongmei.name, madongmei.number)

madongmei.sayHi()

? ? // ---------------------

? ? // 夏洛 100

? ? // 姓名 夏洛 學號 100

? ? // 馬冬梅 101

? ? // 姓名 馬冬梅 學號 101


2.繼承

extends

super


// 父類

class People {

? ? constructor(name) {

? ? ? ? this.name = name

? ? }

? ? eat(something) {

? ? ? ? console.log(`${this.name} eat ${something}`)

? ? }

}

// 子類

class Student extends People {

? ? constructor(name, number) {

? ? ? ? super(name)

? ? ? ? this.number = number

? ? }

? ? sayHi() {

? ? ? ? console.log(`學生 ${this.name} 學號 ${this.number} 打招呼~`)

? ? }

}

// 子類

class Teacher extends People {

? ? constructor(name, major) {

? ? ? ? super(name)

? ? ? ? this.major = major

? ? }

? ? teach() {

? ? ? ? console.log(`${this.name} 教授 ${this.major}`)

? ? }

}

// 實例:學生

const xialuo = new Student('夏洛', 100)

console.log(xialuo.name, xialuo.number)

xialuo.sayHi()

xialuo.eat('蛋糕')

// 實例:老師

const wanglaoshi = new Teacher('王老師', '語文')

console.log(wanglaoshi.name, wanglaoshi.major)

wanglaoshi.teach()

wanglaoshi.eat('茄子')

? ? // ---------------------

? ? // 夏洛 100

? ? // 學生 夏洛 學號 100 打招呼~

? ? // 夏洛 eat 蛋糕

? ? // 王老師 語文

? ? // 王老師 教授 語文

? ? // 王老師 eat 茄子


3.類型判斷-instanceof

instanceof 可以判斷引用類型

Object 是所有類 class 的父類

Student 繼承于 People , People 繼承于 Object


console.log(xialuo instanceof Student)

console.log(xialuo instanceof People)

console.log(xialuo instanceof Object)

console.log([] instanceof Array)

console.log([] instanceof Object)

console.log({}

? ? instanceof Object)


4.原型

4.1 class 實際上是函數(shù),可見是語法糖


console.log(typeof Student)

console.log(typeof People)

? ? // 解釋:

? ? // js的 class 繼承不像 java 純繼承生均,而是原型繼承

? ? // class 本質還是一個 function

console.log(typeof Object)

console.log(typeof Array)


4.2 隱式原型和顯示原型


console.log(xialuo.__proto__)

console.log(Student.prototype)

console.log(xialuo.__proto__ === Student.prototype) // true

xialuo.name

"夏洛"

xialuo.number

100

xialuo.sayHi()

學生 夏洛 學號 100 打招呼~

undefined

xialuo.__proto__

People?{constructor: ?, sayHi: ?}

xialuo.__proto__.sayHi()

學生 undefined 學號 undefined 打招呼~

undefined

xialuo.__proto__.name

undefined

xialuo.__proto__.number

undefined

xialuo.__proto__ === Student.prototype

true


5.原型圖

原型模型圖

6.原型關系

每個 class 都有顯示原型 prototype

每個實例都有隱式原型 __proto__

實例的 __proto__ 指向對應 class 的 prototype


7.基于原型的執(zhí)行規(guī)則

獲取屬性 xialuo.name 或執(zhí)行方法 xialuo.sayHi() 時

先在自身屬性和方法尋找

如果找不到則自動去 __proto__ 中查找


8.原型鏈


console.log(People.prototype === Student.prototype.__proto__)


9.原型鏈圖

原型鏈模型圖

10.向上找

class 里調用屬性調用方法的本質,以及繼承的本質包括如何繼承于 Object 的本質眉撵。


11.instanceof?

順著隱式原型向上找苍日,對應到 class 的顯示原型。


12.JS原型本章相關的面試題

題目解答

? 如何準確判斷一個變量是不是數(shù)組蒲肋?


console.log(a instanceof Array)


? class的原型本質,怎么理解钝满?

答:

原型和原型鏈的圖示(不參考任何自己畫下來)

屬性和方法的執(zhí)行規(guī)則(如何通過鏈向上找兜粘,也很重要)

? 手寫一個簡易的jQuery,考慮插件和擴展性


class jQuery {

? ? constructor(selector) {

? ? ? ? const result = document.querySelectorAll(selector)

? ? ? ? const length = result.length

? ? ? ? for (let i = 0; i < length; i++) {

? ? ? ? ? ? this[i] = result[i]

? ? ? ? }

? ? ? ? this.length = length

? ? ? ? this.selector

? ? }

? ? get(index) {

? ? ? ? return this[index]

? ? }

? ? each(fn) {

? ? ? ? for (let i = 0; i < this.length; i++) {

? ? ? ? ? ? const elem = this[i]

? ? ? ? ? ? fn(elem)

? ? ? ? }

? ? }

? ? on(type, fn) {

? ? ? ? ? ? return this.each(elem => {

? ? ? ? ? ? ? ? elem.addEventListener(type, fn, false)

? ? ? ? ? ? })

? ? ? ? }

? ? ? ? // 擴展很多 DOM API

}

// 插件

jQuery.prototype.dialog = function(info) {

? ? alert(info)

}

// 覆寫“造輪子”

class myJQuery extends jQuery {

? ? constructor(selector) {

? ? ? ? ? ? super(selector)

? ? ? ? }

? ? ? ? // 擴展自己的方法

? ? addClass(className) {

? ? }

? ? style(data) {

? ? }

}


13.myjquery原型鏈模型圖

myjquery原型鏈模型圖
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末弯蚜,一起剝皮案震驚了整個濱河市孔轴,隨后出現(xiàn)的幾起案子墩剖,更是在濱河造成了極大的恐慌掉奄,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,185評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件盏筐,死亡現(xiàn)場離奇詭異收厨,居然都是意外死亡晋柱,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評論 3 393
  • 文/潘曉璐 我一進店門帽氓,熙熙樓的掌柜王于貴愁眉苦臉地迎上來趣斤,“玉大人,你說我怎么就攤上這事黎休∨欤” “怎么了?”我有些...
    開封第一講書人閱讀 163,524評論 0 353
  • 文/不壞的土叔 我叫張陵势腮,是天一觀的道長联贩。 經常有香客問我,道長捎拯,這世上最難降的妖魔是什么泪幌? 我笑而不...
    開封第一講書人閱讀 58,339評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮署照,結果婚禮上祸泪,老公的妹妹穿的比我還像新娘。我一直安慰自己建芙,他們只是感情好没隘,可當我...
    茶點故事閱讀 67,387評論 6 391
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著禁荸,像睡著了一般右蒲。 火紅的嫁衣襯著肌膚如雪阀湿。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,287評論 1 301
  • 那天瑰妄,我揣著相機與錄音陷嘴,去河邊找鬼。 笑死间坐,一個胖子當著我的面吹牛灾挨,可吹牛的內容都是我干的。 我是一名探鬼主播竹宋,決...
    沈念sama閱讀 40,130評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼涨醋,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了逝撬?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 38,985評論 0 275
  • 序言:老撾萬榮一對情侶失蹤乓土,失蹤者是張志新(化名)和其女友劉穎宪潮,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體趣苏,經...
    沈念sama閱讀 45,420評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡狡相,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,617評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了食磕。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片尽棕。...
    茶點故事閱讀 39,779評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖彬伦,靈堂內的尸體忽然破棺而出滔悉,到底是詐尸還是另有隱情,我是刑警寧澤单绑,帶...
    沈念sama閱讀 35,477評論 5 345
  • 正文 年R本政府宣布回官,位于F島的核電站,受9級特大地震影響搂橙,放射性物質發(fā)生泄漏歉提。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,088評論 3 328
  • 文/蒙蒙 一区转、第九天 我趴在偏房一處隱蔽的房頂上張望苔巨。 院中可真熱鬧,春花似錦废离、人聲如沸侄泽。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蔬顾。三九已至宴偿,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間诀豁,已是汗流浹背窄刘。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留舷胜,地道東北人娩践。 一個月前我還...
    沈念sama閱讀 47,876評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像烹骨,于是被迫代替她去往敵國和親翻伺。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,700評論 2 354

推薦閱讀更多精彩內容