donfuego
01-16-2012, 07:33 AM
Hi!
This is probably a classic inheritance thing...
I have two objects, C1 and C2. Both contains a callback method named 'callback'. Now, if I let C2 inherit from C1, then C1's 'callback' gets overridden by C2's. The problem is I still want C1's methods to access their own 'callback' method.
Is this possible, and is it "valid"? Am I headed down disaster lane here? Should I re-think and refactor?
Example:
function C1 () {};
C1.prototype.callback = function () {
console.log('c1 called');
};
C1.prototype.call = function () {
//do stuff
this.callback();
};
function C2 () {};
C2.prototype = new C1();
C2.prototype.callback = function () {
console.log('c2 called');
};
var obj = new C2();
obj.call();
Output:
c2 called
Regards
Don
This is probably a classic inheritance thing...
I have two objects, C1 and C2. Both contains a callback method named 'callback'. Now, if I let C2 inherit from C1, then C1's 'callback' gets overridden by C2's. The problem is I still want C1's methods to access their own 'callback' method.
Is this possible, and is it "valid"? Am I headed down disaster lane here? Should I re-think and refactor?
Example:
function C1 () {};
C1.prototype.callback = function () {
console.log('c1 called');
};
C1.prototype.call = function () {
//do stuff
this.callback();
};
function C2 () {};
C2.prototype = new C1();
C2.prototype.callback = function () {
console.log('c2 called');
};
var obj = new C2();
obj.call();
Output:
c2 called
Regards
Don