我有三个对象,Science,物理和数学。
我希望最后两个对象( Mathematics__)、__、、物理、和)继承Science的原型属性。
然而,我希望数学和物理都能更新继承的属性并定义它们。这已经完成了,但是当我试图通过物理实例访问科学属性和方法时,我一直得到未定义的。我的密码可能有什么问题。
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);
发布于 2020-11-11 16:02:23
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);
https://stackoverflow.com/questions/64789785
复制相似问题