【问题标题】:Inheritance in Object Oriented JavaScript面向对象 JavaScript 中的继承
【发布时间】:2020-11-11 15:56:48
【问题描述】:

我有 3 个对象,SciencePhysicsMathematics

我想要最后两个对象物理数学 继承Science的原型属性。

但我希望 数学物理 都更新继承的属性并定义它们的属性。这已经完成,但是当我尝试通过 Physics 的实例访问 Science 属性和方法时,我不断收到 undefined。我的代码可能有什么问题。

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
Mathematics.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 Physics
let mechanics = new Physics();
mechanics.name = "mechanics";
mechanics.subFields = ["linear", "force", "force fileds"];

log(mechanics.universal);

【问题讨论】:

  • 将父类实例化为子类的原型很奇怪,在某些情况下可能会咬你!我建议您将Mathematics.prototype = new Science() 替换为Mathematics.prototype = Object.create(Science.prototype)
  • Mathematics.prototype =Object.create(Science.prototype); Physics.prototype = Object.create(Science.prototype); Physics.prototype.constructor = Physics;

标签: javascript oop object prototypal-inheritance


【解决方案1】:
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);

【讨论】:

  • Object.assign(Physics.prototype, { name: "Physics", dificulty: "80%", type: "Physical Science", subFields: ["Electricity", "Mechanics", "Sound", "Optics", "Waves"], }); 返回未定义
  • 我在我的答案中添加了一个 sn-p,它成功使用了 Object.assign。
猜你喜欢
  • 1970-01-01
  • 2012-09-23
  • 2013-02-22
  • 2013-07-20
  • 2019-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多