The problem is that in my inherit method where I setup the prototype I don't get the class properties passed over. What I thought I would do was to just loop trough them with a For In loop but then I remembered that won't work because then the class B would have copies or references to the variables of the ones in A. But what I want is that when a class property of A is changed in B then it is also changed in A. To put it simply, they are one and the same object/data in memory.
The result I want is this one:
A.myValue = "Hello"; B.myValue += " world!"; print(A.myValue); // output as 'Hello world!'The result should work seamless and the developer shouldn't even know what goes on.
Here's my code so far.
Function.prototype.inherit = function inherit(parent) {
for(var prop in parent) {
setter = new Function("val", "return this.prototype.class." + prop + " = val;");
this.__defineSetter__(prop, setter);
setter.name = prop;
getter = new Function("return this.prototype.class." + prop + ";");
this.__defineGetter__(prop, getter);
getter.name = prop;
}
this.prototype = new parent;
this.prototype.class = parent;
this.prototype.parent = this.prototype;
}
function A() {
func = A;
while(func.caller) {
func = func.caller;
}
this.class = func;
}
function B() {
A.call(this);
}
A.prototype.super = function super() {
name = super.caller.name;
func = this.class.prototype.class.prototype[name];
if(func == undefined)
throw name + " in " + this.parent.toString() + " not found!";
return func.apply(this, arguments);
}
A.toString = function toString() {
return this.name;
}
A.prototype.toString = function toString() {
str = "<" + this.class.name + "#";
for(var property in this) {
value = this[property];
str += property;
if(value instanceof A) {
str += " => " + "<";
str += value.class.name + "#...";
str += ">";
} else if(value instanceof Function) {
str += "()";
} else {
str += " = ";
if(value instanceof String) {
str += '"' + value + '"';
} else {
str += value;
}
}
str += ", ";
}
str = str.slice(0, str.length-2);
str += ">";
return str;
}
A.prototype.test = function test(a) {
print(a);
}
A.wtf = "Hello world!";
B.inherit(A);
B.prototype.test = function test(a) {
this.super(a);
}
b = new B;
b.test("Hey! This is awesome!!!!");
print(b);
print(b.parent);
print(B.wtf);
B.wtf = "Bye bye world!";
print(A.wtf);
If it matter, I am using the V8 engine.
**EDIT**
Never mind! I managed to solve it myself. Here's the Function.inherit function that solves it all! What I do is that I define setters and getters that wrap around the values of the previous "class". Enjoy:
Function.prototype.inherit = function inherit(parent) {
for(var prop in parent) {
setter = new Function("val", "return this.prototype.class." + prop + " = val;");
this.__defineSetter__(prop, setter);
setter.name = prop;
getter = new Function("return this.prototype.class." + prop + ";");
this.__defineGetter__(prop, getter);
getter.name = prop;
}
this.prototype = new parent;
this.prototype.class = parent;
this.prototype.parent = this.prototype;
}
Edited by Groogy, 28 August 2010 - 10:13 AM.
solved it


Sign In
Create Account


Back to top









