Hi!
So far I have been doing "inheritanc e" in Javascript like this:
******
function TestObject(valu e)
{
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. 8 1446
Robert wrote: Hi!
So far I have been doing "inheritanc e" in Javascript like this:
****** function TestObject(valu e) { 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.
VK wrote: Robert wrote: Hi!
So far I have been doing "inheritanc e" in Javascript like this:
****** function TestObject(valu e) { 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.
Robert <ro****@noreply .x> writes: So far I have been doing "inheritanc e" in Javascript like this:
****** function TestObject(valu e) { 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.prot otype, 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.prot otype 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(TestObjec t.prototype);
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Lasse Reichstein Nielsen wrote: Robert <ro****@noreply .x> writes:
So far I have been doing "inheritanc e" in Javascript like this:
****** function TestObject(valu e) { 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.prot otype 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(TestObjec t.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.prot otype;
?
In article <44************ ***********@new s.xs4all.nl>, Robert
<ro****@noreply .x> writes Hi!
So far I have been doing "inheritanc e" in Javascript like this:
****** function TestObject(valu e) { 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 ProtoExtendedOb ject()
{
// Attach method functions here
}
ProtoExtendedOb ject.prototype = new ProtoTestObject (); // Proto chain
ExtendedObject. prototype = new ProtoExtendedOb ject();
******
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
Robert <ro*@secret.xyz > writes: Lasse Reichstein Nielsen wrote: Then you can do: ExtendedObject. prototype = clone(TestObjec t.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.prot otype; ?
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.prot otype]
|
v
[Object.prototyp e]
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.prot otype 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/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
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(TestObjec t.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.prot otype;
If one did what you suggest, then
ExtendedObject. prototype.myPro perty = '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.
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(TestObjec t.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.prot otype;
If one did what you suggest, then
ExtendedObject. prototype.myPro perty = '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 :) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
The extend function allows the following:
1) inheriting from multiple classes,
2) inheriting an inherited classes inheritances (awkward to say),
3) inheriting appended prototype methods and properties of inhertited
classes.
|
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 examples?
|
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 parameters, I get CS1501 (no method
with X arguments). Here's a simplified example which mimics the
circumstances:
namespace InheritError {
// Random base class.
public class A {
protected int i;
|
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 so, cannot be called a WebForm
itself, the child pages will implement forms).
I created a Master.aspx page and removed all HTML from it, added some code
to the .aspx.vb file to add controls to my page. Then I created a Child.aspx
and changed the...
|
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 kind of inheritance:
singletonObj = new function() {
this.prop = true;
}
Here are two ways to create a singleton with inheritance:
| |
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
}
|
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 think of several
ad-hoc ways to achieve inheritance, but is there an accepted standard idiom?
--
John W. Kennedy
"But now is a new thing which is very old--
that the rich make themselves richer and not poorer,
which is the true Gospel, for...
|
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 do the following...
|
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
on it's own. However when I call the child object I get an error
" has no properties (in firefox)."
I simple test:
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |