Prototype for private sub-methods

天涯浪子 提交于 2019-12-31 06:49:09

问题


I have code that looks like this:

var baseClass = function() {
// CODE
    var subClass = function() {
        // MORE CODE
    }
}

Adding methods to baseClass is fine, I just use

baseClass.prototype.newMethod = function () {
    // NEW CODE
}

My question is how should I add methods to subClass? Is the only way to simply make it a public method?

######## EDIT ##############

OK so I've rearranged the code so the subClass is outside the baseClass. I pass in baseClass so subClass can still access the properties of the instance of baseClass.

var baseClass = function() {
    var base = this;
    this.property_a = 1;
    this.property_b = 5;

    var sub = new subClass(base);
    // CODE
}

var subClass = function(parent) {
    var sub = this;
    this.property_c = 1;
    this.method_a = function() {
        return sub.property_c + parent.property_a;
    }
    // MORE CODE
}

this is fine and works, but now I have a new problem of when I add a method using prototype:

subClass.prototype.method_b = function(){
    return sub.property_c + parent.property_b;
}

I get an error saying parent isn't defined.

Basically I have a fairly simple web application that has two sides, a viewing side and an editing side. I build the base class which includes everything necessary for viewing, and I want to add the methods required for editing in a different file so they're only loaded when a user is on the editing page.


回答1:


Why do you declare that subclass in the base class? Doesn't make sense to me.

You can add to the subclass's prototype whereever it is in you scope. In your code it would be

var baseClass = function() {
// CODE
    var subClass = function() {
        // MORE CODE
    }
    subClass.prototype = {
        ...
    }
}

But I'd suggest to put it out of the base class constructor. If you want it private for some reason, add a closure:

(function(){

    baseClass = function() { // public
        // CODE
    }
    baseClass.prototype = {...};

    var subClass = function() { // private
        // MORE CODE
    }
    subClass.prototype = Object.create(baseClass.prototype);
    subClass.prototype.newMethod = function () { ... }
})()

EDIT to answer the extended question:

Ah, subClass doesn't inherit from baseClass! We had expected that, otherwise it may be OK to have it inside the constructor. Then, the same prototype could have been added to each of the different subClass constructors:

var subproto = {
    method_b: = function(){
        // manipulate "this"
    },
    ...
};
function baseClass() {
    // some code
    function sub() {
        // is a constructor for subs which belong to this specif base intance
        ...
    }
    sub.prototype = subproto; // the sub constructors of each base instance
                              // have the same prototype
    var x = new sub(),
        y = new sub(); // example usage of the sub constructor
}
baseClass.prototype = {...}

Else, if you want one common sub constructor (outside of function baseClass), you may give the base instance the sub belongs to as an argument to the constructor - as you did. Of course the sub (both internal and external methods) can only access public properties of that base instance.

The mistake you made in your rearranged code is that your prototype ("external") methods tried to access the private parent variable from the sub constructor. As you say, "error saying parent isn't defined".

var subClass = function(parent) {
    var sub = this;
    this.parent = parent; // make it public
    this.property_c = 1;
    this.method_a = function() {
        return sub.property_c + parent.property_a;
    }
    // MORE CODE
}
subClass.prototype.method_b = function(){
    // prototype functions can only access public properties
    // i.e. privileged methods, public attributes and other prototype properties
    return this.property_c + this.parent.property_b;
}



回答2:


You will have to define the methods in the same context as you define subClass:

var baseClass = function() {
    // CODE
    var subClass = function() {
        // MORE CODE
    }
    subClass.prototype.newMethod = function () { ... }
}

If that's not possible, then you will need to expose subClass to the appropriate context or provide a mechanism from baseClass to extend subClass.




回答3:


If you really want to keep the subclass private, you could hide the definitions in a closure:

var BaseClass = (function() {
    function BaseClass() { ... };
    function SubClass() { ... };
    BaseClass.prototype.foo = function() { ... };
    SubClass.prototype.foo = function() { ... };
    return BaseClass;
})();

I have personally found this kind of closure-enforced protection to be more trouble than it's worth (ex, makes debugging more difficult)… But if you wanted to do it, that's how you would.



来源:https://stackoverflow.com/questions/9982562/prototype-for-private-sub-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!