Take the 2-minute tour ×

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.

share|improve this question
I don't understand the question .. what are you trying to achieve ? –  c69 Jan 25 '12 at 17:48
I put it in nice bold font. –  Brian Graham Jan 25 '12 at 17:53
add comment

2 Answers

up vote 1 down vote accepted

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:

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.

share|improve this answer
Thanks for finding a close-enough way to approach my question, this works for me. –  Brian Graham Jan 26 '12 at 14:35
add comment

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

Stop doing that.

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


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.

share|improve this answer
although I kind of agree here, that's not realy an answer. –  gion_13 Jan 25 '12 at 18:01
add comment

Your Answer


