how to inherit from a class together with constructor

D

Daniel

Hi folks,

I've read all the javascript resources (well, not all, but a lot :))
on inheritance, prototipical and classical, including Douglas
Crockford's javascript site but still I don't get something. Maybe
it's impossible in which case a clear answer to this effect from a
knowledgeable person would be great. I know it's not terribly useful
to think about classes in javascript but apparently this analogy will
not go away so I'll use it too.

Suppose I have a class with some initialization code and would like to
subclass this *together with* the initialization code. In other words
in the subclass I don't want to rewrite the initialization (or
constructor).

If it helps I would do this in python:

class Person:
def __init__( self, dictionary ):
self.options = dictionary
def method( self ):
pass

class Employee( Person ):
pass

e = Employee( somedictionary ) # I don't have to specify how many
arguments are taken
# upon
instantiation or what to do with these arguments
# because this is
defined already in Person
print e.options # this would print the
content of somedictionary

But it seems to me that in javascript I can't do this. This is what I
tried:

function Person( dictionary ) { this.options = dictionary; }
Person.prototype.method = function( ) { }

function Employee( ) { }
Employee.constructor = Person.constructor;
// also tried Employee.constructor = new Person.constructor;

var e = new Employee( { a: 'a', b: 'b' } )
alert( e.options.a ) // gives an error

Note that the inheritance of properties and methods works perfectly
well also in javascript but It seems that as soon as I write function
Employee( ){ } the initialization (or constructor) code from Person is
gone. What is the way to go then?
 
D

David Mark

Hi folks,

I've read all the javascript resources (well, not all, but a lot :))
on inheritance, prototipical and classical, including Douglas
Crockford's javascript site but still I don't get something. Maybe
it's impossible in which case a clear answer to this effect from a
knowledgeable person would be great. I know it's not terribly useful
to think about classes in javascript but apparently this analogy will
not go away so I'll use it too.

Suppose I have a class with some initialization code and would like to
subclass this *together with* the initialization code. In other words
in the subclass I don't want to rewrite the initialization (or
constructor).

If it helps I would do this in python:

class Person:
    def __init__( self, dictionary ):
        self.options = dictionary
    def method( self ):
        pass

class Employee( Person ):
    pass

e = Employee( somedictionary )    # I don't have to specify how many
arguments are taken
                                                     # upon
instantiation or what to do with these arguments
                                                     # because this is
defined already in Person
print e.options                               # this would print the
content of somedictionary

But it seems to me that in javascript I can't do this. This is what I
tried:

function Person( dictionary ) { this.options = dictionary; }
Person.prototype.method = function( ) { }

function Employee( ) { }
Employee.constructor = Person.constructor;
// also tried Employee.constructor = new Person.constructor;

function Employee(dictionary) {
Person.call(this, dictionary);
}

Employee.prototype = new Person();
 

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
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top