473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Overriding a constructor

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
Sep 11 '08 #1
10 1659
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:
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
Sep 11 '08 #2
CAFxX wrote:
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
Why not have a second class that extends the first class. In the second
class you could have a method A and in its constructor you could have
$this->A();
Sep 11 '08 #3
sheldonlg wrote:
CAFxX wrote:
>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

Why not have a second class that extends the first class. In the second
class you could have a method A and in its constructor you could have
$this->A();
Oops, I thought I was in a different newsgroup. I don't know about
classes in javascript.
Sep 11 '08 #4
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/
Sep 11 '08 #5
sheldonlg meinte:
Oops, I thought I was in a different newsgroup. I don't know about
classes in javascript.
No one knows.

Gregor
--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Sep 11 '08 #6
Gregor Kofler wrote:
sheldonlg meinte:
>Oops, I thought I was in a different newsgroup. I don't know about
classes in javascript.

No one knows.
That depends on what is meant by "javascript" ;-)
SCNR

PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Sep 11 '08 #7
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:
sheldonlg meinte:
>Oops, I thought I was in a different newsgroup. I don't know about
classes in javascript.

No one knows.

Gregor

Sep 11 '08 #8
hmmm, I don't get what OldA.apply is supposed to be...

Joost Diepenmaat ha scritto:
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.
Sep 11 '08 #9
CAFxX <us********@REMOVEMEcafxx.cjb.netwrites:
hmmm, I don't get what OldA.apply is supposed to be...
somefunction.apply() calls the function passing a specific "this" and
arguments.

See, for example:

http://developer.mozilla.org/en/Core...Function/apply
--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Sep 11 '08 #10
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
--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Sep 11 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Christoph Groth | last post by:
-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.6 (GNU/Linux) Comment: For info see http://www.gnupg.org iD8DBQBAJmKufx2/njzvX5URAipnAKCUco5ZCl85xJ8YboHHJM3RBIqiGwCfX3/P...
9
by: James Marshall | last post by:
I'm writing a library where I want to override document.write(), but for all document objects; thus, I want to put it in the prototype. I tried Document.prototype.write= my_doc_write ; but it...
3
by: Cheng Mo | last post by:
When overriding operator new & delte of one class, the method is implicitly declared as static. However, overriding operator new & delete of template cannot be static.The compiler says cannot...
2
by: Samer | last post by:
I'm writing a class that derives from ServiceBase and it says in the documentation for the constructor of ServiceBase that if you override the base class constructor, you should explicitly call it...
7
by: Greg | last post by:
I have a base class for a label. I want to set a property at the base class level, say AutoSize = true. Then at the form level I want to be able to overide the base class setting so that I can...
4
by: TS | last post by:
i have a class that i'm trying to understand that overrides BaseApplicationException's methods as follows. What i dont' understand is that i have never seen the inherit ":" on a method signature,...
2
by: project | last post by:
Hi every body, Any body can help me the following doubts? 1. what is constructor? 2. what is destructor? 3. what is overriding function. 4. different between structure and array 5. what is...
1
by: Carl Fenley | last post by:
I've been programming exclusively in C# for the last few years but am now working on a project where I am required to write all code in VB.NET. I'm trying to create a class with multiple...
6
by: bryanbabula | last post by:
I have a question about overriding i was wondering if anyone could help me with, or even suggesting a better/different way. I have no idea if this can even be done or not. I was wondering if there...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.