CAFxX <us********@REMOVEMEcafxx.cjb.netwrites:
I'm trying to solve a problem with my code but can't come up with a
solution.
Suppose I have a "class" A with its constructor (that I can't modify
directly)
function A() {
/* constructor code */
};
what I'd like to do is to make the constructor call a certain function
I specify before returning, i.e.
function A() {
/* constructor code */
someFunction();
};
is this possible?
CAFxX
You mean you want to replace the constructor A at runtime with some other
function that calls the original A and then adds some more
functionality?
var OldA = A;
A = function() {
OldA.apply(this,arguments);
// do new stuff here
};
// copy any properties set on OldA
for (p in OldA) {
if (OldA.hasOwnProperty(p)) {
A[p] = OldA[p];
}
// this may fix some potential issues
// and break some others
A.prototype.constructor = A;
}
If the original code tries hard enough, it can always detect that (and
break because) the constructor been replaced, and if there are already
instances of A around at the time the new constructor is created those
instances will be instanceOf OldA, not A.
see also:
http://joost.zeekat.nl/constructors-...confusing.html
HTH,
Joost.
--
Joost Diepenmaat | blog:
http://joost.zeekat.nl/ | work:
http://zeekat.nl/