Possible Duplicate:
Crockford’s Prototypal inheritance – Issues with nested objects
I’m having a problem getting the following code to execute a function in prototype B from prototype A, and was wondering if there was any easy solution:
var Ob = function () {
test = 'hi';
}
Ob.prototype.A = {
that : this,
root : this,
goB : function () {
var that = this;
console.log('hello');
that.B.wtf();
}
}
Ob.prototype.B = {
that : this,
root : this,
wtf : function () {
var that = this;
console.log(that);
}
}
test = new Ob;
test.A.goB();
You need to wire up your properties after the object has been created:
A rather dirty hack is to use a privileged method in the parent object to augment the child object and return it so you have access to the parent object via a property. The dirty part is that if you cache the object the property is not guaranteed to have the correct value. So this is more or less a way to do it although you really shouldn’t do it this way.