为了账号安全,请及时绑定邮箱和手机立即绑定

面向对象 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 关注
  • 111 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信