在JavaScript中,如何从子类中的父类继承,使用单个“.prototype”块? [英] In JavaScript, how do I inherit from a parent class in child class using a single ".prototype" block?

查看:156
本文介绍了在JavaScript中,如何从子类中的父类继承,使用单个“.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("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块如果可能。

But I'd like to continue to use style from the first example and have all my class methods defined in a single ".prototype" block if possible.

推荐答案

看看下面的答案StackOverflow : http://stackoverflow.com/a/17893663/783743

Take a look at the following answer on StackOverflow: http://stackoverflow.com/a/17893663/783743

这个答案介绍了原型类同构的概念。把它简单的一个原型对象可以用来建模一个类。以下代码取自上述答案:

This answer introduces the concept of prototype-class isomorphism. To put it simply a prototype object can be used to model a class. The following code is taken from the above answer:

function CLASS(prototype) {
    var constructor = prototype.constructor;
    constructor.prototype = prototype;
    return constructor;
}

使用上面的方法我们可以实现 code>如下:

Using the above method we can implement Person as follows:

var Person = CLASS({
    constructor: function () {
        this.name = "my name";
    },
    sayHello: function () {
        console.log("Hello, I am a person.");
    },
    sayGoodbye: function () {
        console.log("Goodbye");
    }
});

但是继承需要一些额外的工作。所以让我们修改 CLASS 函数:

Inheritance however requires some additional work. So let's modify the CLASS function a little:

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;
}

我们还需要定义 descriptorOf 函数 CLASS 工作:

We also need to define the descriptorOf function for CLASS to work:

function descriptorOf(object) {
    return Object.keys(object).reduce(function (descriptor, key) {
        descriptor[key] = Object.getOwnPropertyDescriptor(object, key);
        return descriptor;
    }, {});
}

现在我们可以创建 Student 如下:

Now we can create Student as follows:

var Student = CLASS({
    constructor: function () {
        this.studentId = 0;
    },
    sayHello: function () {
        console.log("Hello, I am a student.");
    }
}, Person);

查看演示: http://jsfiddle.net/CaDu2/

如果您需要任何帮助理解代码,请随时与我联系。

If you need any help understanding the code then feel free to contact me.

这篇关于在JavaScript中,如何从子类中的父类继承,使用单个“.prototype”块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆