第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定

為什么我在給子類添加原型方法后無法調(diào)用父類原型上的方法

function Person(name, age) {

this.name = name || "person";

this.age = age || 0;

}

Person.prototype = {

LEG_NUM:2,

ARM_NUM:2,

sayHi: function () {

console.log("my name is" + this.name + "my age is " + this.age + "years old");

},

walking: function () {

console.log(this.name + "is walking");

}

}

function Student(name, age, classname) {

Person.call(this, name, age);

this.classname = classname;

}

Student.prototype = Object.create(Person.prototype);

Student.prototype.constructor = Student;

Student.prototype = {

sayHi: function () {

console.log("my name is " + this.name + "my age is " + this.age + "my class is " + this.classname);

},

learn: function (obj) {

console.log(this.name + "is learning..." + obj);

}

}

var leo = new Student("leo", 12, "class 2,grade 3");

leo.walking();

leo.sayHi();

leo.learn("math");

console.log(leo.LEG_NUM);

console.log(leo.ARM_NUM);


https://img1.sycdn.imooc.com//5bb598da00011d5904990177.jpg

去掉子類的原型方法就能用了????

正在回答

2 回答

剛看了書,樓上說的極是,不能使用對象字面量創(chuàng)建原型方法,這樣會(huì)重寫原型鏈。

0 回復(fù) 有任何疑惑可以回復(fù)我~

Student.prototype = {

sayHi: function () {

console.log("my name is " + this.name + "my age is " + this.age + "my class is " + this.classname);

},

learn: function (obj) {

console.log(this.name + "is learning..." + obj);

}

}

這一句不能這么寫,這樣等于把Student.prototype的值更改為后面所定義的對象,而不是父類的實(shí)例,包括你上面定義的constructor屬性的定義也會(huì)失效,應(yīng)該寫成:

Student.prototype.sayHi=function () {

console.log("my name is " + this.name + "my age is " + this.age + "my class is " + this.classname);

};

Student.prototype.learn=function (obj) {

console.log(this.name + "is learning..." + obj);

};

切記:不要用字面量寫法,因?yàn)樗鼤?huì)整個(gè)替換掉prototype所指向的對象



1 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

為什么我在給子類添加原型方法后無法調(diào)用父類原型上的方法

我要回答 關(guān)注問題
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)