473,385 Members | 1,317 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,385 software developers and data experts.

Inheritance in Javascript question

Hi!

So far I have been doing "inheritance" in Javascript like this:

******
function TestObject(value)
{
this.value = value;
}

function ExtendedObject(value1, value2)
{
TestObject.call(this, value1);
this.value2 = value2;
}

ExtendedObject.prototype = new TestObject();
******

This works okay, but I have a wrong feeling with the TestObject
constructor being called just to "inherit" from it and a second time
again later. Especially if the TestObject function really expected a
value parameter. Is there a way so I can "inherit" without doing
ExtendedObject.prototype = new TestObject();

I looked at http://www.crockford.com/javascript/inheritance.html
but the constructor is called twice in that framework too.
May 1 '06 #1
8 1420
VK

Robert wrote:
Hi!

So far I have been doing "inheritance" in Javascript like this:

******
function TestObject(value)
{
this.value = value;
}

function ExtendedObject(value1, value2)
{
TestObject.call(this, value1);
this.value2 = value2;
}

ExtendedObject.prototype = new TestObject();
******

This works okay, but I have a wrong feeling with the TestObject
constructor being called just to "inherit" from it and a second time
again later. Especially if the TestObject function really expected a
value parameter. Is there a way so I can "inherit" without doing
ExtendedObject.prototype = new TestObject();


Well, you may decide first what kind of inheritance you want to use -
because the above looks like a holly cocktail of both methods :-)

Either you have a set of independent constructors extending and
inheriting each other in any defined way (what is what called
"parasiting inheritance" in one article I read which is a sure way for
a heart attack for C/Java programmers :-)

Or you are patching the prototype chain of a single constructor until
it starts producing needed objects.

I see no reason to use both ways at once - unless you want to
complicate your own life.

May 1 '06 #2
VK wrote:
Robert wrote:
Hi!

So far I have been doing "inheritance" in Javascript like this:

******
function TestObject(value)
{
this.value = value;
}

function ExtendedObject(value1, value2)
{
TestObject.call(this, value1);
this.value2 = value2;
}

ExtendedObject.prototype = new TestObject();
******

This works okay, but I have a wrong feeling with the TestObject
constructor being called just to "inherit" from it and a second time
again later. Especially if the TestObject function really expected a
value parameter. Is there a way so I can "inherit" without doing
ExtendedObject.prototype = new TestObject();


Well, you may decide first what kind of inheritance you want to use -
because the above looks like a holly cocktail of both methods :-)


I don't really see why the above would look like a cocktail, but if you
have a better way please show it. I really don't mind which method it is.

May 1 '06 #3
Robert <ro****@noreply.x> writes:
So far I have been doing "inheritance" in Javascript like this:

******
function TestObject(value)
{
this.value = value;
}

function ExtendedObject(value1, value2)
{
TestObject.call(this, value1);
this.value2 = value2;
}

ExtendedObject.prototype = new TestObject();
******

This works okay, but I have a wrong feeling with the TestObject
constructor being called just to "inherit" from it and a second time
again later.


I concur.

What I would do is to create an object extending from
TestObject.prototype, and use that as ExtendedObject.prototype.
That is kindof what you do, except that the function you use
to create the extending object also initializes the new object.
We don't want that.

So, we create another function with TestObject.prototype and
no other effect, and use that to extend the object. This
is a general operation, so let's make a function that does it:

function clone(object) {
function Dummy(){};
Dummy.prototype = object;
return new Dummy();
}

Then you can do:
ExtendedObject.prototype = clone(TestObject.prototype);

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
May 1 '06 #4
Lasse Reichstein Nielsen wrote:
Robert <ro****@noreply.x> writes:
So far I have been doing "inheritance" in Javascript like this:

******
function TestObject(value)
{
this.value = value;
}

function ExtendedObject(value1, value2)
{
TestObject.call(this, value1);
this.value2 = value2;
}

ExtendedObject.prototype = new TestObject();
******


So, we create another function with TestObject.prototype and
no other effect, and use that to extend the object. This
is a general operation, so let's make a function that does it:

function clone(object) {
function Dummy(){};
Dummy.prototype = object;
return new Dummy();
}

Then you can do:
ExtendedObject.prototype = clone(TestObject.prototype);


Looks interesting.
I'm trying hard to understand what happens in this code, but my mind
can't understand it fully.
How is this different compared to directly doing
ExtendedObject.prototype = TestObject.prototype;
?
May 1 '06 #5
In article <44***********************@news.xs4all.nl>, Robert
<ro****@noreply.x> writes
Hi!

So far I have been doing "inheritance" in Javascript like this:

******
function TestObject(value)
{
this.value = value;
}

function ExtendedObject(value1, value2)
{
TestObject.call(this, value1);
this.value2 = value2;
}

ExtendedObject.prototype = new TestObject();
Why do you want a prototype that holds data (the value property)? It's
no use to you.

A prototype object is really a different type, so why not give it a
separate constructor :

function ProtoExtendedObject()
{
// Attach method functions here
}

ProtoExtendedObject.prototype = new ProtoTestObject(); // Proto chain
ExtendedObject.prototype = new ProtoExtendedObject();

******

This works okay, but I have a wrong feeling with the TestObject
constructor being called just to "inherit" from it and a second time
again later. Especially if the TestObject function really expected a
value parameter. Is there a way so I can "inherit" without doing
ExtendedObject.prototype = new TestObject();

I looked at http://www.crockford.com/javascript/inheritance.html
but the constructor is called twice in that framework too.


--
John Harris
May 1 '06 #6
Robert <ro*@secret.xyz> writes:
Lasse Reichstein Nielsen wrote:

Then you can do:
ExtendedObject.prototype = clone(TestObject.prototype);


I'm trying hard to understand what happens in this code, but my mind
can't understand it fully.
How is this different compared to directly doing
ExtendedObject.prototype = TestObject.prototype;
?


The difference is that there is another object in the prototype chain.
The prototype chain of the new ExtendedObject is:

[new cloned object]
|
v
[TestObject.prototype]
|
v
[Object.prototype]
You don't use it here, but you could use it to add properties to
ExtendedObject.prototype that is shared by all instances of
ExtendedObject, but is not shared by TestObject instances.

If you just assign TestObject.prototype directly to
ExtendedObject.prototype, you can't add properties to only
ExtendedObject's prototype.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
May 1 '06 #7
On 01/05/2006 20:42, Robert wrote:
Lasse Reichstein Nielsen wrote:


[snip]
function clone(object) {
function Dummy(){};
Dummy.prototype = object;
return new Dummy();
}

Then you can do:
ExtendedObject.prototype = clone(TestObject.prototype);


Looks interesting.
I'm trying hard to understand what happens in this code, but my mind
can't understand it fully.
How is this different compared to directly doing
ExtendedObject.prototype = TestObject.prototype;


If one did what you suggest, then

ExtendedObject.prototype.myProperty = 'value';

would not only change the prototype object for ExtendedObject, but also
that of TestObject as both prototype properties reference, rather than
copy, the same object.

Lasse's code creates a new object specifically for the task of acting as
the prototype object of ExtendedObject. This prevents such interference.

Mike
--
Michael Winter
Prefix subject with [News] before replying by e-mail.
May 1 '06 #8
Michael Winter wrote:
On 01/05/2006 20:42, Robert wrote:
Lasse Reichstein Nielsen wrote:

[snip]
function clone(object) {
function Dummy(){};
Dummy.prototype = object;
return new Dummy();
}

Then you can do:
ExtendedObject.prototype = clone(TestObject.prototype);

Looks interesting.
I'm trying hard to understand what happens in this code, but my mind
can't understand it fully.
How is this different compared to directly doing
ExtendedObject.prototype = TestObject.prototype;

If one did what you suggest, then

ExtendedObject.prototype.myProperty = 'value';

would not only change the prototype object for ExtendedObject, but also
that of TestObject as both prototype properties reference, rather than
copy, the same object.


Thank you! Now it all makes sense :)

May 2 '06 #9

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

Similar topics

5
by: Robert Spoons | last post by:
Can you look over this code, preferably try it, and comment? I believe the 'extend' function below will allow you to use full 'class inheritance' in javascript, but I would like to verify it. ...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
6
by: VR | last post by:
Hi, I read about Master Pages in ASP.Net 2.0 and after implementing some WinForms Visual Inheritance I tryed it with WebForms (let's say .aspx pages, my MasterPage does not have a form tag itself...
2
by: Kevin Newman | last post by:
I have been playing around with a couple of ways to add inheritance to a JavaScript singleton pattern. As far as I'm aware, using an anonymous constructor to create a singleton does not allow any...
4
by: Christopher Benson-Manica | last post by:
Is this code both correct and canonical? function foo( arg ) { this.initialize( arg ); } foo.prototype.initialize=function( arg ) { // Initialize based on arg }
31
by: John W. Kennedy | last post by:
I quite understand about prototypes and not having classes as such, but I happen to have a problem involving blatant is-a relationships, such that inheritance is the bloody obvious way to go. I can...
14
by: petermichaux | last post by:
Hi, Hopefully the group doesn't mind an(other) inheritance question. Maybe the prototype inheritance style is starting to become a low dim light in my brain...probably not yet. ---- If I...
36
by: Pacific Fox | last post by:
Hi all, haven't posted to this group before, but got an issue I can't work out... and hoping to get some help here ;-) I've got a base object that works fine with named arguments when called...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.