在JavaScript中,如何使用单个“继承”从子类中的父类继承;。原型;块
例如:在JavaScript中,如何使用单个“继承”从子类中的父类继承;。原型;块,javascript,class,inheritance,prototype,Javascript,Class,Inheritance,Prototype,例如: function Person() { //person properties this.name = "my name"; } Person.prototype = { //person methods sayHello: function() { console.log("Hello, I am a person."); } sayGoodbye: function() { console.log("
function Person() {
//person properties
this.name = "my name";
}
Person.prototype = {
//person methods
sayHello: function() {
console.log("Hello, I am a person.");
}
sayGoodbye: function() {
console.log("Goodbye");
}
}
function Student() {
//student specific properties
this.studentId = 0;
}
Student.prototype = {
//I need Student to inherit from Person
//i.e. the equivalent of
//Student.prototype = new Person();
//Student.prototype.constructor = Person;
//student specific methods
//override sayHello
sayHello: function() {
console.log("Hello, I am a student.");
}
}
我知道我可以通过以下方式实现这一点:
function Student() {
this.studentId = 0;
}
Student.prototype = new Person();
Student.prototype.constructor = Person;
Student.prototype.sayHello = function () {
console.log("Hello, I am a student.");
}
但我希望继续使用第一个示例中的样式,并尽可能在单个“.prototype”块中定义我的所有类方法。查看以下有关StackOverflow的答案: 这个答案引入了原型类同构的概念。简单地说,可以使用原型对象对类进行建模。以下代码取自上述答案:
function CLASS(prototype) {
var constructor = prototype.constructor;
constructor.prototype = prototype;
return constructor;
}
使用上述方法,我们可以实现Person
,如下所示:
var Person = CLASS({
constructor: function () {
this.name = "my name";
},
sayHello: function () {
console.log("Hello, I am a person.");
},
sayGoodbye: function () {
console.log("Goodbye");
}
});
var Student = CLASS({
constructor: function () {
this.studentId = 0;
},
sayHello: function () {
console.log("Hello, I am a student.");
}
}, Person);
然而,继承需要一些额外的工作。因此,让我们稍微修改一下类
函数:
function CLASS(prototype, base) {
switch (typeof base) {
case "function": base = base.prototype;
case "object": prototype = Object.create(base, descriptorOf(prototype));
}
var constructor = prototype.constructor;
constructor.prototype = prototype;
return constructor;
}
我们还需要定义描述符
函数,以便类
工作:
function descriptorOf(object) {
return Object.keys(object).reduce(function (descriptor, key) {
descriptor[key] = Object.getOwnPropertyDescriptor(object, key);
return descriptor;
}, {});
}
现在我们可以创建Student
,如下所示:
var Person = CLASS({
constructor: function () {
this.name = "my name";
},
sayHello: function () {
console.log("Hello, I am a person.");
},
sayGoodbye: function () {
console.log("Goodbye");
}
});
var Student = CLASS({
constructor: function () {
this.studentId = 0;
},
sayHello: function () {
console.log("Hello, I am a student.");
}
}, Person);
请亲自观看演示:
如果您需要了解代码的任何帮助,请随时与我联系。查看以下有关StackOverflow的答案: 这个答案引入了原型类同构的概念。简单地说,可以使用原型对象对类进行建模。以下代码取自上述答案:
function CLASS(prototype) {
var constructor = prototype.constructor;
constructor.prototype = prototype;
return constructor;
}
使用上述方法,我们可以实现Person
,如下所示:
var Person = CLASS({
constructor: function () {
this.name = "my name";
},
sayHello: function () {
console.log("Hello, I am a person.");
},
sayGoodbye: function () {
console.log("Goodbye");
}
});
var Student = CLASS({
constructor: function () {
this.studentId = 0;
},
sayHello: function () {
console.log("Hello, I am a student.");
}
}, Person);
然而,继承需要一些额外的工作。因此,让我们稍微修改一下类
函数:
function CLASS(prototype, base) {
switch (typeof base) {
case "function": base = base.prototype;
case "object": prototype = Object.create(base, descriptorOf(prototype));
}
var constructor = prototype.constructor;
constructor.prototype = prototype;
return constructor;
}
我们还需要定义描述符
函数,以便类
工作:
function descriptorOf(object) {
return Object.keys(object).reduce(function (descriptor, key) {
descriptor[key] = Object.getOwnPropertyDescriptor(object, key);
return descriptor;
}, {});
}
现在我们可以创建Student
,如下所示:
var Person = CLASS({
constructor: function () {
this.name = "my name";
},
sayHello: function () {
console.log("Hello, I am a person.");
},
sayGoodbye: function () {
console.log("Goodbye");
}
});
var Student = CLASS({
constructor: function () {
this.studentId = 0;
},
sayHello: function () {
console.log("Hello, I am a student.");
}
}, Person);
请亲自观看演示:
如果您需要任何帮助来理解代码,请随时与我联系。嗯,这里有一个可能会有帮助的……嗯,这里有一个可能会有帮助的。。。