如何在JS中实现继承?揭示原型? [英] How to implement inheritance in JS Revealing prototype pattern?

查看:99
本文介绍了如何在JS中实现继承?揭示原型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何继承/扩展正在使用显示原型模式的类?
有没有办法使私人变量和函数保护

How do I inherit/extend classes that are using the Revealing Prototype pattern? And is there a way to make the private variables and functions protected?

示例基础对象:

myNameSpace.Person = function() {

    this.name= "";
    this.id = 0;

};

myNameSpace.Person.prototype = function(){
    var foo = function(){
        //sample private function
    };
    var loadFromJSON = function (p_jsonObject) {
       ...
    };
    var toJSON = function () {
       ...
    };
    var clone = function (p_other) {
       ...
    };

    return {
        loadFromJSON : loadFromJSON,
        toJSON: toJSON,
        clone: clone
    };
}();


推荐答案

JavaScript中没有受保护的变量/属性。虽然,当您在同一范围内声明继承类时,您可以重用私有变量,这在您的情况下似乎可能是私有变量只是您的原型的隐藏实用程序。

There are no protected variables/properties in JavaScript. Though, you can reuse "private" variables when you declare the inheriting classes in the same scope, which seems possible in your case when the private variables are only "hidden utilities" of your prototype.

MyNamespace.Person = function Person(params) {
    // private variables and functions, individual for each Person instance
    var anything, id;
    function execute_something() {}

    // public properties:
    this.name = "";
    this.getId = function getId(){
        // called a "privileged function", because it has access to private variables
    }
}
MyNamespace.American = function(params) {
    MyNamespace.Person.call(this, params); // inherit name and getId()
}

(function() { // new scope for
    // hidden utility functions and other private things
    function foo() { }
    function helpJSON() { }
    function fromJSON() { }
    var bar;

    (function(personProto) { // new scope for prototype module (not explicitly needed)
        // "private" /static/ variables (and functions, if you want them private)
        var personCount = 0;

        personProto.clone = function clone() {
            return this.constructor(myself); // or something
        };
        personProto.toJSON = function toJSON() {
            // use of helpJSON()
        };
        personProto.fromJSON = fromJSON; // direct use
    })(MyNamespace.Person.prototype);

    (function(amiProto) {
        // just the same as above, if needed
        amiProto.special = function() {
            // use foo() and co
        };
    })( MyNamespace.American.prototype = Object.create(MyNamespace.Person.prototype) );
})();

这是继承的JavaScript方法,这意味着美国的原型继承了clone(),toJSON()和JSON()从Person的原型自动运行。当然可以覆盖。而且该功能是

This is the JavaScript way of inheritance, which means American's prototype inherits the clone(), toJSON() and fromJSON() functions automagically from the Person's prototype. Of course overwritable. And the feature is

new MyNamespace.American() instanceof MyNamespace.Person; // true

当然,如果你不需要,而且要使用更多的模块 - 像这样,你可以重用这个实用程序的功能,就是复制它们:

Of course, if you don't need that, and want use the more module-like way, you could reuse the utility functions, i.e. just copy them:

(function() {
    // hidden utility functions and other private things
    var bar;
    var personCount;
    function foo() { }
    function helpJSON() { }
    function fromJSON() { }
    function clone() {
        return this.constructor(myself); // or something
    }
    function toJSON() { }

    (function(personProto) { // new scope, not really needed
        // private variables are useless in here
        personProto.clone = clone;
        personProto.toJSON = toJSON;
        personProto.fromJSON = fromJSON;
    })(MyNamespace.Person.prototype);

    (function(amiProto) { // new scope, not really needed
        // copied from personProto
        amiProto.clone = clone;
        amiProto.toJSON = toJSON;
        amiProto.fromJSON = fromJSON;
        // and now the differences
        amiProto.special = function() {
            // use foo() and co
        };
    })(MyNamespace.American.prototype);
})();

这篇关于如何在JS中实现继承?揭示原型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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