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

Inheritance question

Is this code both correct and canonical?

function foo( arg ) {
this.initialize( arg );
}

foo.prototype.initialize=function( arg ) {
// Initialize based on arg
}

function myFoo( arg ) {
this.initialize( arg );
// Set other custom properties of myFoo
}

myFoo.prototype=new foo( '' ); // type of arg irrelevant here
myFoo.prototype.constructor=myFoo;

Basically I want to call the superclass constructor from the subclass'
constructor and then do some initialization of my own. In case it
helps, in C++ I would do something like

class B : public A {
B( int arg ) : A( arg ) {/*whatever*/}
};

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Dec 16 '05 #1
4 1339
> Basically I want to call the superclass constructor from the subclass'
constructor and then do some initialization of my own.


My advice is to not try to apply C++ patterns to JavaScript. It will
only break your heart. JavaScript is a prototypal language, not a
classical language. It is flexible enough to simulate classical
patterns, but that is not what it is good at. JavaScript even tries to
look somewhat classical with that contructor jazz, but I think that it
is just confusing, as you have reported.

You will be more effective working with prototypes directly. First,
you need a function that makes a new empty object that inherits from
an existing one. This feature should be in the language but isn't, but
that's ok because it is really easy to write:

function object(o) {
function f() {}
f.prototype = o;
return new f();
}

So,
b = object(a);

The new b appears to have all of the properties of a, but it isn't a
copy. It inherits. This is class-free inheritance.

Having that in hand, you can make an object that is like what you
want, and make lots of instances that point to it and inherit from it.

You can have maker functions that return a specialized object. You can
have a makerB function that calls a makerA function, taking its
objectA result, augmenting it, and returning it as an objectB. I call
this Parasitic Inheritance.

http://www.crockford.com/javascript
Dec 16 '05 #2
VK

Christopher Benson-Manica wrote:
Is this code both correct and canonical?

function foo( arg ) {
this.initialize( arg );
}

foo.prototype.initialize=function( arg ) {
// Initialize based on arg
}

function myFoo( arg ) {
this.initialize( arg );
// Set other custom properties of myFoo
}

myFoo.prototype=new foo( '' ); // type of arg irrelevant here
myFoo.prototype.constructor=myFoo;

Basically I want to call the superclass constructor from the subclass'
constructor and then do some initialization of my own. In case it
helps, in C++ I would do something like

class B : public A {
B( int arg ) : A( arg ) {/*whatever*/}
};

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.


JavaScript is *not* Java, but the background "thinking" yet closer to
Java than to C++ because guys who did the first prototype had Java book
on their tables. It is not my relevation - you may read LiveScript
story yourselve.

/* Java */

public class Employee {
static String store = "ACME, Inc.";
String name = "John Doe";
}

public class Manager extends Employee {
super();
public String department = "Dep. 001";
}
/* JavaScript */
function Employee() {
this.name = "John Doe";
}
Employee.prototype.store = "ACME, Inc.";

function Manager() {
Employee.call(this);
this.department = "dep. 001";
}

Dec 16 '05 #3
VK

Douglas Crockford wrote:
You can
have a makerB function that calls a makerA function, taking its
objectA result, augmenting it, and returning it as an objectB. I call
this Parasitic Inheritance.


With all my respect you would call it just "Inheritance" w/o any
epithets. This is how Inheritance is described in C++, Java and other
languages.

If ObjectA do not fully satisfy your needs, you don't patch or hack it.
You leave the old horse (ObjectA) alone, you create new object ObjectB,
inherit all useful stuff from ObjectA (super object) and extend ObjectB
with missing properties/methods.
This is how nearly all native classes in C++ / Java are build - and it
would be too loud to call these languages "a set of parasiting
classes".

Prototype change/replacement was the only way until well official but
long neglected call() and apply() methods went into wide use and
support.
But now there is a possibility to work in the conventional OOP
paradigm, so why not follow it?

Unless I'm missing something crucial.

Dec 17 '05 #4
> Unless I'm missing something crucial.

So it would seem.

Dec 18 '05 #5

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

Similar topics

1
by: KK | last post by:
Windows Forms Inheritance, Incomplete? I was playing around with Windows Forms and found out this Forms Inheritance feature. The moment I saw that, I felt this can be used effectively if the...
2
by: KK | last post by:
** Posting it here cause after couple of days no body responded.** I was playing around with Windows Forms and found out this Forms Inheritance feature. The moment I saw that, I felt this can...
4
by: Dave Theese | last post by:
Hello all, The example below demonstrates proper conformance to the C++ standard. However, I'm having a hard time getting my brain around which language rules make this proper... The error...
8
by: __PPS__ | last post by:
Hello everybody, today I had another quiz question "if class X is privately derived from base class Y what is the scope of the public, protected, private members of Y will be in class X" By...
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...
5
by: Noah Roberts | last post by:
Is there anything that says that if you virtually inherit from one class you have to virtually inherit from anything you inherit from?
3
by: RSH | last post by:
I have a simple question regarding inheritance in a web form. I have a DropDownList in an aspx form. It is called DropDownList1 I have a class that will be overriding the render event so I...
8
by: RSH | last post by:
Hi, I am working on some general OOP constructs and I was wondering if I could get some guidance. I have an instance where I have a Base Abstract Class, and 4 Derived classes. I now need to...
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: 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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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: 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...

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.