简体   繁体   中英

JavaScript: Inheritance by prototype, without using prototype to define/call methods

I've been making several libraries and extension libraries, and it's not practical for me to use prototype because in the real-world you actually have private variables that do not need to be accessed from an instantiated object.

var parent_class = function(num) {
    var number = num;

    this.method = function(anum) {
        number += anum;
    };
    this.getNumber = function() {
        var fnumber = number + 2;
        return fnumber;
    };
};

var child_class = function(num1, num2) {
    var sum = num1 + num2;
    parent_class.call(this, sum); // initialize parent class

    this.method = function() {
        sum -= 1;
        base.method(sum); // what to do here
    };
};

child_class.prototype = new parent_class(); // inherit from parent
child_class.prototype.constructor = child_class; // maintain new constructor

var c = new child_class(1, 4); // child_class.sum = 5, parent_class.number = 5
c.method(); // child_class.sum = 4, parent_class.number = 9
var num = c.getNumber(); // returns 11

Now, without declaring and relying on methods being prototyped, how can I get the what to do here line to work? I understand that I could call this.method(); inside child_class and store it as a variable, but I want overridable methods .

My previous topic was not very forthcoming, as everyone assumes I can just prototype everything or use private variables to get around the problem. There has to be a way to do this without prototyping.

I want to call .method(); from inside an instance of child_class without storing the previous .method(); as a private variable or using prototype methods that, for obvious reasons, cannot access private members .

You're trying to make JavaScript into something it's not.

Stop doing that.

JavaScript does inheritance through the prototype. Get used to it.

ADDENDUM

Okay, let's clarify.

JavaScript doesn't implement OOP like C++, Java, C#, VB.NET, or any other object oriented language. You have the prototype. You can extend the prototype. If you want to "override a method", you need a reference to the version that previously existed.

Now, the caveat is going to be that anyone, at any time, can come along and replace anything, anywhere. So any assumptions about the stability of your object model are flimsy, at best.

Let the prototype do its job. Any framework you build around it to try to mimic inheritance, abstract base classes, superclasses, and what not, is going to have its own headaches and become a maintenance nightmare.

Just use the prototype, you can mark "private" properties and methods with NULL character prefix or whatever to discourage people from using them, which is all you really need.

MyClass.prototype = {

    "\0privateMethod": function(){}

};

If you're using chrome you will see this in console:

Object
privateMethod: function (){}
__proto__: Object

Yet one cannot do myClass.privateMethod() , this should be enough of a hint that this is a private property. To actually call it, you'd need to write myClass["\\0privateMethod"]() .

After going back to using prototype, your problem should automatically become easy to solve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM