Overriding a constructor

C

CAFxX

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
 
C

CAFxX

Just to clarify, the aim is that all the code that is instantiating an
instance of "class" A, perform the call to someFunction() without rewrites.

CAFxX ha scritto:
 
J

Joost Diepenmaat

CAFxX said:
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-considered-mildly-confusing.html

HTH,
Joost.
 
C

CAFxX

Right, sarcasm never dies.
I am _obviously_ talking about the prototype mechanism (notice the
quotes around "class" in the first post).

Gregor Kofler ha scritto:
 
C

CAFxX

hmmm, I don't get what OldA.apply is supposed to be...

Joost Diepenmaat ha scritto:
CAFxX said:
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-considered-mildly-confusing.html

HTH,
Joost.
 
G

Gregor Kofler

CAFxX meinte:
Right, sarcasm never dies.
I am _obviously_ talking about the prototype mechanism (notice the
quotes around "class" in the first post).

I noticed and I wasn't replying to you.

Gregor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,139
Messages
2,570,805
Members
47,356
Latest member
Tommyhotly

Latest Threads

Top