書(shū)上的解釋不太理解,求大神幫解釋下。
為什么第一種沒(méi)有繼承到sides屬性
// 創(chuàng)建作用域安全的構(gòu)造函數(shù)
function Polygon(sides) {
if (this instanceof Polygon) {
console.log('this', this);
this.sides = sides;
this.getArea = function(){
return 0;
}
} else {
return new Polygon(sides);
}
}
// 非作用域安全的構(gòu)造函數(shù)
function Rectangle(width, height) {
Polygon.call(this, 2);
this.width = width;
this.height = height;
this.getArea = function (){
return this.width * this.height;
}
}
let rect = new Rectangle(5,10);
console.log(rect.sides);
為什么rect沒(méi)有繼承到side屬性
而通過(guò)原型就可以
Rectangle.prototype = new Polygon();
let rect = new Rectangle(2,4);
console.log(rect.sides);// 2
JavaScript 的繼承是通過(guò)原型鏈實(shí)現(xiàn)。
沒(méi)有下面的語(yǔ)句:
Rectangle.prototype = new Ploygon();
無(wú)法實(shí)現(xiàn) Rectangle 繼承 Polygon。
在 Rectangle 函數(shù)中,Polygon.call(this,2) 只是相當(dāng)于調(diào)用了基類(lèi)的構(gòu)造函數(shù)。但是前提是,你要先實(shí)現(xiàn)繼承,沒(méi)有繼承關(guān)系,這個(gè)調(diào)用沒(méi)啥效果。
寫(xiě)在注釋里了
// 創(chuàng)建作用域安全的構(gòu)造函數(shù)
function Polygon(sides) {
if (this instanceof Polygon) {
debugger; // (3) 此時(shí) this 是 Polygon 實(shí)例,所以執(zhí)行到了這里
console.log('this', this);
this.sides = sides;
this.getArea = function(){
return 0;
}
} else {
debugger; // (1) 調(diào)用 Polygon.call(this, 2) 時(shí) this 不是 Polygon 的實(shí)例 所以先執(zhí)行到了這里
return new Polygon(sides); // (2) 此時(shí)返回了一個(gè) Polygon實(shí)例
}
}
// 非作用域安全的構(gòu)造函數(shù)
function Rectangle(width, height) {
// 執(zhí)行(2)返回了一個(gè)Polygon實(shí)例 但是你沒(méi)有用到 也就是沒(méi)有賦給變量 所以在 new Rectangle(5,10) 時(shí)候不會(huì)繼承sides
Polygon.call(this, 2);
this.width = width;
this.height = height;
this.getArea = function (){
return this.width * this.height;
}
}
let rect = new Rectangle(5,10);
console.log(rect);
// Rectangle 的原型鏈直接指向了一個(gè) Polygon 實(shí)例, 那么new Rectangle(5,10)時(shí)候, 所有的Rectangle實(shí)例都會(huì)繼承sides,因?yàn)椴檎覍?duì)象屬性時(shí)如果在當(dāng)前對(duì)象上找不到,就會(huì)順著原型鏈一直找
Rectangle.prototype = new Polygon();
// 比如這個(gè)例子
let obj = { a:1 };
obj.__proto__.b = 2; // 屬性b在obj的原型鏈上
console.log(obj); // { a: 1 }
console.log(obj.b); // 2
第一種情況,當(dāng)你let rect = new Rectangle(5,10);時(shí),此時(shí)rect是Rectangle的一個(gè)實(shí)例,Polygon.call(this, 2); 中this指代rect,你把rect作為Polygon的this來(lái)調(diào)用Polygon,當(dāng)在Polygon中判斷(this instanceof Polygon)這句話(huà)時(shí),發(fā)現(xiàn)this也就是rect不是Polygon的實(shí)例,根據(jù)instanceof的用法它會(huì)查找this的原型鏈,原型鏈中是否有Polygon的實(shí)例,如果找到就返回true。顯然沒(méi)找到,所以實(shí)際上Polygon.call(this, 2);這句話(huà)相當(dāng)于執(zhí)行了new Polygon(2),與rect無(wú)關(guān),所以rect中沒(méi)有sides屬性。
第二中情況:Rectangle.prototype = new Polygon();你讓Rectangle.prototype變?yōu)镻olygon的一個(gè)實(shí)例(這其實(shí)是javascript的原型繼承),看個(gè)例子:
function FOO(){}
function Foo(){}
Foo.prototype = new FOO();
var foo = new Foo(); //
foo.__proto__===Foo.prototype;
console.log(foo instanceof Foo)//true
console.log(foo instanceof FOO)//true,根據(jù)foo的原型鏈找到了FOO的實(shí)例
因此第二種情況中Polygon.call(this, 2);它執(zhí)行的是if語(yǔ)句中的代碼,把sides和getArea放到了this中,所以rect.sides會(huì)是2。
北大青鳥(niǎo)APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專(zhuān)業(yè)的國(guó)家
北大青鳥(niǎo)中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過(guò)二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國(guó)一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國(guó)成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開(kāi)發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_(kāi)發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開(kāi)發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開(kāi)發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開(kāi)發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問(wèn),美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。