面向对象中的继承 JavaScript
Inheritance in Object Oriented JavaScript
我有 3 个对象,科学、物理 和数学。
我想要最后两个对象 (物理 和 数学)
继承科学.
的原型属性
但我希望 数学 和 物理 都更新继承的属性并定义它们。这已经完成,但是当我尝试通过 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);
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);
我有 3 个对象,科学、物理 和数学。
我想要最后两个对象 (物理 和 数学) 继承科学.
的原型属性但我希望 数学 和 物理 都更新继承的属性并定义它们。这已经完成,但是当我尝试通过 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);
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);