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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

面向?qū)ο?JavaScript 中的繼承

面向?qū)ο?JavaScript 中的繼承

烙印99 2023-07-29 13:48:46
我有 3 個(gè)對(duì)象:科學(xué)、物理和數(shù)學(xué)。我希望最后兩個(gè)對(duì)象(物理和數(shù)學(xué)) 繼承科學(xué)的原型屬性。然而我希望數(shù)學(xué)和物理都更新繼承的屬性并定義它們的屬性。這已經(jīng)完成,但是當(dāng)我嘗試通過Physics實(shí)例訪問Science屬性和方法時(shí),我總是得到未定義的信息。我的代碼可能有什么問題。function log(elem) {  return console.log(elem);}//create supertype => Sciencefunction Science() {}//define Science prototype propsScience.prototype = {  constructor: Science,  dificulty: "Variable",  universal: true,  type: "science",  name: "science",  hasSubFields() {    return true;  },};//create 2 sub fields : Mathematics and Physics to inherit props from Sciencefunction Mathematics(subField) {  this.subField = subField;}function Physics() {}//let mathematics & Physics inherit science propsMathematics.prototype = Object.create(Science.prototype);Physics.prototype = Object.create(Science.prototype);Physics.prototype.constructor = Physics;//over write Mathematics inherited props and physicsMathematics.prototype = {  constructor: Mathematics,  name: "Mathematics",  type: "Pure and applied Science",};Physics.prototype = {  name: "Physics",  dificulty: "80%",  type: "Physical Science",  subFields: ["Electricity", "Mechanics", "Sound", "Optics", "Waves"],};//make instance of Physicslet mechanics = new Physics();mechanics.name = "mechanics";mechanics.subFields = ["linear", "force", "force fileds"];log(mechanics.universal);
查看完整描述

1 回答

?
繁華開滿天機(jī)

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超4個(gè)贊

Physics.prototype = new Science();


//...


Physics.prototype = {

  name: "Physics",

  dificulty: "80%",

  type: "Physical Science",

  subFields: ["Electricity", "Mechanics", "Sound", "Optics", "Waves"],

};

第二行將覆蓋第一行。代碼完成后,原型就是新對(duì)象。與 不再有任何關(guān)系Science,因此沒有universal財(cái)產(chǎn)可以繼承。


您不需要替換prototype,而是需要添加:


Physics.prototype = new Science();


//...


Physics.prototype.name = "Physics";

Physics.prototype.dificulty = "80%";

Physics.prototype.subFields = "Physical Science";

Physics.prototype.name = ["Electricity", "Mechanics", "Sound", "Optics", "Waves"];

或者:


Physics.prototype = new Science();


//...


Object.assign(Physics.prototype, {

  name: "Physics",

  dificulty: "80%",

  type: "Physical Science",

  subFields: ["Electricity", "Mechanics", "Sound", "Optics", "Waves"],

});

Mathematics將需要類似的改變。


function log(elem) {

  return console.log(elem);

}

//create supertype => Science

function Science() {}


//define Science prototype props

Science.prototype = {

  constructor: Science,

  dificulty: "Variable",

  universal: true,

  type: "science",

  name: "science",

  hasSubFields() {

    return true;

  },

};


//create 2 sub fields : Mathematics and Physics to inherit props from Science

function Mathematics(subField) {

  this.subField = subField;

}

function Physics() {}


//let mathematics & Physics inherit science props

Mathematics.prototype = Object.create(Science.prototype);

Physics.prototype = Object.create(Science.prototype);

Physics.prototype.constructor = Physics;


//over write Mathematics inherited props and physics

Object.assign(Mathematics.prototype, {

  constructor: Mathematics,

  name: "Mathematics",

  type: "Pure and applied Science",

});


Object.assign(Physics.prototype, {

  name: "Physics",

  dificulty: "80%",

  type: "Physical Science",

  subFields: ["Electricity", "Mechanics", "Sound", "Optics", "Waves"],

})


//make instance of Physics

let mechanics = new Physics();

mechanics.name = "mechanics";

mechanics.subFields = ["linear", "force", "force fileds"];


log(mechanics.universal);


查看完整回答
反對(duì) 回復(fù) 2023-07-29
  • 1 回答
  • 0 關(guān)注
  • 157 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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