首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >面向对象JavaScript中的继承

面向对象JavaScript中的继承
EN

Stack Overflow用户
提问于 2020-11-11 15:56:48
回答 1查看 44关注 0票数 0

我有三个对象,Science物理数学

我希望最后两个对象( Mathematics__)、__、、物理、和)继承Science的原型属性。

然而,我希望数学和物理都能更新继承的属性并定义它们。这已经完成了,但是当我试图通过物理实例访问科学属性和方法时,我一直得到未定义的。我的密码可能有什么问题。

代码语言:javascript
复制
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);

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-11 16:02:23

代码语言:javascript
复制
Physics.prototype = new Science();

//...

Physics.prototype = {
  name: "Physics",
  dificulty: "80%",
  type: "Physical Science",
  subFields: ["Electricity", "Mechanics", "Sound", "Optics", "Waves"],
};

第二行是覆盖第一行。一旦代码完成,原型就是新的对象。不再存在与Science的任何关系,因此没有universal属性可继承。

与其替换prototype,还需要添加以下内容:

代码语言:javascript
复制
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"];

或者:

代码语言:javascript
复制
Physics.prototype = new Science();

//...

Object.assign(Physics.prototype, {
  name: "Physics",
  dificulty: "80%",
  type: "Physical Science",
  subFields: ["Electricity", "Mechanics", "Sound", "Optics", "Waves"],
});

Mathematics将需要类似的更改。

代码语言:javascript
复制
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);

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64789785

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档