亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

面向對象 JavaScript 中的繼承

面向對象 JavaScript 中的繼承

烙印99 2023-07-29 13:48:46
我有 3 個對象:科學、物理和數學。我希望最后兩個對象(物理和數學) 繼承科學的原型屬性。然而我希望數學和物理都更新繼承的屬性并定義它們的屬性。這已經完成,但是當我嘗試通過Physics實例訪問Science屬性和方法時,我總是得到未定義的信息。我的代碼可能有什么問題。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 回答

?
繁華開滿天機

TA貢獻1816條經驗 獲得超4個贊

Physics.prototype = new Science();


//...


Physics.prototype = {

  name: "Physics",

  dificulty: "80%",

  type: "Physical Science",

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

};

第二行將覆蓋第一行。代碼完成后,原型就是新對象。與 不再有任何關系Science,因此沒有universal財產可以繼承。


您不需要替換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);


查看完整回答
反對 回復 2023-07-29
  • 1 回答
  • 0 關注
  • 129 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號