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.
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.
If you’re using chrome you will see this in console:
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 writemyClass["\0privateMethod"]().After going back to using prototype, your problem should automatically become easy to solve.