I have 3 objects, Science, Physics and Mathematics.
I want the last two object (Physics and Mathematics) to inherit the protoype properties of Science.
Yet I want both Mathematics & Physics to update the inherited properties and define theirs. This is done, but I keep getting undefined when I try to access the Science properties and method via an instance of Physics. What could be wrong with my code.
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()withMathematics.prototype = Object.create(Science.prototype)Mathematics.prototype =Object.create(Science.prototype); Physics.prototype = Object.create(Science.prototype); Physics.prototype.constructor = Physics;