原型式繼承中構造器被覆蓋的問題

看下面代碼

function Person(){}
function Man(){}
function Woman(){}

Woman.prototype = new Person
var a = new Man
var b = new Woman
a.constructor
b.constructor

問 a.constructor和b.constructor分別是什么占遥?

a.constructor
-> function Man(){}
b.constructor
-> function Person(){}

會不會覺得 b.constructor = function Person(){} 好像不太對勁呢?

接下來你會干的一件事情是什么呢颊糜?既然是通過new對應的構造函數(shù)得到的對象,我猜你應該會打印下面的語句

Man.constructor
-> function Function() { [native code] }
Woman.constructor
-> function Function() { [native code] }
Person.constructor
-> function Function() { [native code] }

可能你會反應個2-3秒秃踩,然后哈哈一笑衬鱼,Man是函數(shù),函數(shù)不都是Function構造的嗎憔杨!

現(xiàn)在距離真相就只差一步之遙了鸟赫,接下來做什么呢?

Man.prototype.constructor
-> function Man(){}
Woman.prototype.constructor
-> function Person(){}
Person.prototype.constructor
-> function Person(){}

原來 Woman.prototype = new Person這個操作把Woman自己的constructor屬性覆蓋了,為什么說是覆蓋了呢抛蚤?因為constructor屬性在函數(shù)定義之時就已經(jīng)存在了台谢。

這樣當b訪問constructor屬性時在當前的Women的prototype上就找不到對應的constructor屬性,進而就會向上訪問對象的原型鏈岁经,最終找到的是Person對象的constructor屬性对碌。

既然知道了原因,要修正這個問題就簡單了

Woman.prototype.constructor = Woman 
-> function Woman(){}
var c = new Woman
-> undefined
c.constructor
-> function Woman(){}

new 操作符

*ECMA262中對new操作符有下面的定義 *

11.2.2 The new Operator

The production NewExpression : new NewExpression is evaluated as follows:

  • Let ref be the result of evaluating NewExpression.
  • Let constructor be GetValue(ref).
  • If Type(constructor) is not Object, throw a TypeError exception.
  • If constructor does not implement the [[Construct]] internal method, throw a TypeErrorexception.
  • Return the result of calling the [[Construct]] internal method on constructor, providing no arguments (that is, an empty list of arguments).
  • The production MemberExpression : new MemberExpression Arguments is evaluated as follows:
  • Let ref be the result of evaluating MemberExpression.
  • Let constructor be GetValue(ref).
  • Let argList be the result of evaluating Arguments, producing an internal list of argument values (11.2.4).
  • If Type(constructor) is not Object, throw a TypeError exception.
  • If constructor does not implement the [[Construct]] internal method, throw a TypeErrorexception.
  • Return the result of calling the [[Construct]] internal method on constructor, providing the listargList as the argument values.

上面的意思就是先求NewExpression的值為ref蒿偎,然后在ref上調用內部的[[construct]]方法朽们,還是比較抽象是吧,stackoverflow上有人給出了new操作符的執(zhí)行流程诉位。

var _new = function(fn) {
  var obj = Object.create(fn.prototype);
  var result = fn.apply(obj);
  return result != null && typeof result === 'object' ? result : obj;
};
  1. 看到了吧骑脱,和上面我們實驗的結果是一致的。Object.create 的只是fn的原型prototype苍糠,由于原型中丟失了constructor屬性叁丧,所以我們在訪問的時候就會出現(xiàn)找到了父親的constructor屬性。

  2. 再看_new中的fn.apply(obj)岳瞭,new Woman的時候fn是Woman構造函數(shù)呢還是Person構造函數(shù)呢拥娄,這個我們驗證一下.

    function A(){ console.log('in A'); }
    -> undefined
    function B(){ console.log('in B'); }
    -> undefined
    B.prototype = new A();
    -> in A 
    -> A {}
    b = new B()
    -> in B 
    -> B {}
    

符合預期。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末瞳筏,一起剝皮案震驚了整個濱河市稚瘾,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌姚炕,老刑警劉巖摊欠,帶你破解...
    沈念sama閱讀 218,640評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異柱宦,居然都是意外死亡些椒,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,254評論 3 395
  • 文/潘曉璐 我一進店門掸刊,熙熙樓的掌柜王于貴愁眉苦臉地迎上來免糕,“玉大人,你說我怎么就攤上這事忧侧∈ぃ” “怎么了?”我有些...
    開封第一講書人閱讀 165,011評論 0 355
  • 文/不壞的土叔 我叫張陵苍柏,是天一觀的道長尼斧。 經(jīng)常有香客問我,道長试吁,這世上最難降的妖魔是什么棺棵? 我笑而不...
    開封第一講書人閱讀 58,755評論 1 294
  • 正文 為了忘掉前任楼咳,我火速辦了婚禮,結果婚禮上烛恤,老公的妹妹穿的比我還像新娘母怜。我一直安慰自己,他們只是感情好缚柏,可當我...
    茶點故事閱讀 67,774評論 6 392
  • 文/花漫 我一把揭開白布苹熏。 她就那樣靜靜地躺著,像睡著了一般币喧。 火紅的嫁衣襯著肌膚如雪轨域。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,610評論 1 305
  • 那天杀餐,我揣著相機與錄音干发,去河邊找鬼。 笑死史翘,一個胖子當著我的面吹牛枉长,可吹牛的內容都是我干的。 我是一名探鬼主播琼讽,決...
    沈念sama閱讀 40,352評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼必峰,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了钻蹬?” 一聲冷哼從身側響起吼蚁,我...
    開封第一講書人閱讀 39,257評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎脉让,沒想到半個月后桂敛,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,717評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡溅潜,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,894評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了薪伏。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片滚澜。...
    茶點故事閱讀 40,021評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖嫁怀,靈堂內的尸體忽然破棺而出设捐,到底是詐尸還是另有隱情,我是刑警寧澤塘淑,帶...
    沈念sama閱讀 35,735評論 5 346
  • 正文 年R本政府宣布萝招,位于F島的核電站,受9級特大地震影響存捺,放射性物質發(fā)生泄漏槐沼。R本人自食惡果不足惜曙蒸,卻給世界環(huán)境...
    茶點故事閱讀 41,354評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望岗钩。 院中可真熱鬧纽窟,春花似錦、人聲如沸兼吓。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,936評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽视搏。三九已至审孽,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間浑娜,已是汗流浹背佑力。 一陣腳步聲響...
    開封第一講書人閱讀 33,054評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留棚愤,地道東北人搓萧。 一個月前我還...
    沈念sama閱讀 48,224評論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像宛畦,于是被迫代替她去往敵國和親瘸洛。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,974評論 2 355

推薦閱讀更多精彩內容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,509評論 0 23
  • 你在馳而不息次和! 他在弛而不息反肋。 你是個勞累命! 他是個叫不醒踏施。 2017年8月8日
    窗外白楊閱讀 383評論 8 8
  • 第一次在網(wǎng)上寫東西石蔗,心里太煩了,賭博真的有那么上癮嗎畅形?父親沉迷賭博养距,弄的家里雞犬不寧,賭點小的沒關系日熬,父親總是去賭...
    不知忘閱讀 186評論 0 0