473,491 Members | 2,205 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

trouble passing parameters of a subclass constructor through to it's superclass constructor

Hi,

I am having trouble passing parameters of a Javascript subclass
constructor through to it's superclass constructor.

I am trying all sorts of things, including the below, but nothing
worked so far.

Thanks for any help!


function Base(a)
{
this.a = a;
}

function Derived(a)
{
this.prototype.constructor.call(a);
}

Derived.prototype = new Base();
Derived.prototype.constructor = Derived;

var derived = new Derived("hello");
//a should be "hello" now, but it is undefined:
window.alert(derived.a)

May 11 '06 #1
4 3870
VK

ingoweiss wrote:
Hi,

I am having trouble passing parameters of a Javascript subclass
constructor through to it's superclass constructor.

I am trying all sorts of things, including the below, but nothing
worked so far.

Thanks for any help!


function Base(a)
{
this.a = a;
}

function Derived(a)
{
this.prototype.constructor.call(a);
}

Derived.prototype = new Base();
Derived.prototype.constructor = Derived;

var derived = new Derived("hello");
//a should be "hello" now, but it is undefined:
window.alert(derived.a)


Be careful with an a la class inheritance in the conventional
JavaScript (thus not JScript.Net and such). Be careful exactly because
it's an "a la class inheritance", not "the class inheritance".
Everything can work pretty close to what you may used to in other
languages, but many features are emulated or missing.
And for sure you shouldn't use both prototype inheritance and class
inheritance together. Fists of all it's a needless mess, secondly it's
error prone, finally (as you just discovered) it simply doesn't work in
the way you think it should work.

// by keeping (a la) class inheritance:

function Base(a) {
this.foo = 'bar';
this.a = a;
}

function Derived(a,b) {
Base.call(this,a);
this.b = b;
}

var obj = new Derived('a','b');
alert(obj.foo);
alert(obj.a);
alert(obj.b);

May 11 '06 #2

ingoweiss wrote:

[snip]
function Derived(a)
{
this.prototype.constructor.call(a);
} [/snip]

[snip] Derived.prototype.constructor = Derived;

[/snip]

The last line of code sets the value of the "constructor" property of
"Derived.prototype" as a reference to "Derived".

So in your expression "this.prototype.constructor.call", you calling
Derived, not Base.

Regards

Julian Turner

May 11 '06 #3
Julian Turner wrote:
ingoweiss wrote:
[snip]
function Derived(a)
{
this.prototype.constructor.call(a);
}

[/snip]

[snip]
Derived.prototype.constructor = Derived;

[/snip]

The last line of code sets the value of the "constructor"
property of "Derived.prototype" as a reference to "Derived".

So in your expression "this.prototype.constructor.call",
you calling Derived, not Base.


Not really. When - new Derived("hello"); - is executed the - this -
keyword refers to the object being constructed. That object is not a
function. It does not have a - prototype - property, so no function is
called because of the run-time error generated when -
this.prototype.constructor - is referenced.

If the function was called it seems that it would be the wrong function,
but passing in the string primitive "hello" to the function's - call -
method would be worthless as a temporary Sting object should be created
for it, passed into the method to act as the - this - object in the
function call, and then be thrown away as no references to it would
remain.

Richard.
May 12 '06 #4

Richard Cornford wrote:
Not really. When - new Derived("hello"); - is executed the - this -
keyword refers to the object being constructed. That object is not a
function. It does not have a - prototype - property,
called because of the run-time error generated when -
this.prototype.constructor - is referenced. If the function was called it seems that it would be the wrong function,
but passing in the string primitive "hello" to the function's - call -
method would be worthless as a temporary Sting object should be created
for it, passed into the method to act as the - this - object in the
function call, and then be thrown away as no references to it would
remain.


Thank you, yes I forgot those two points. I was too focused on the
fact that the "constructor" property of Derived's prototype was not
even pointing to Base, let alone other problems with the code.

Indeed, if the OP had omitted the "prototype" from the statement, and
properly included a reference to an object in the "call" method thus:-

"this.constructor.call(this,a);"

a stack overflow would have obviously resulted.

The OP may (I am guessing) have been thinking of a pattern along the
lines of the Java "super" keyword, as an alternative to the patten
suggested by VK:-

function Base(a)
{
this.myProp=a;
}

function Derived(a)
{
/* this.superClassConstructor.call(this,a) */
/* or more simply ... */

this.superClassConstructor(a);
}

Derived.prototype=new Base();
Derived.prototype.constructor=Derived;
Derived.prototype.superClassConstructor=Base;

Regards

Julian Turner

May 12 '06 #5

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

Similar topics

11
3442
by: Brent | last post by:
I'd like to subclass the built-in str type. For example: -- class MyString(str): def __init__(self, txt, data): super(MyString,self).__init__(txt) self.data = data
0
1462
by: flupke | last post by:
I have the following test code setup, trying to get the class name of a subclass in the super class. (Reason why i want this is described below) file class_name_start.py ========================...
4
11966
by: bonono | last post by:
Hi, Suppose my class definition is like this : class A: name = "A" @classmethod def foo(cls): cls.__super.foo()
2
4306
by: ToChina | last post by:
Hi, I have the following code: class A { } class B : A { }
5
3293
by: spike grobstein | last post by:
So, I've got this project I'm working on where the app defines various classes that are subclassed by module packages that act like plugins... I'd like the packages to define a file path for...
1
2850
by: shivapadma | last post by:
1..In java we can refer superclass constructor by super()keyword,but Where as in c++ how can we refer to that???. In java the following code is used for referring superclass...
2
2354
by: Mark Brading | last post by:
Hi all, I am trying to declare a Map object with an abstract superclass (TableFields_v2) object as follows:- private Map<Integer, TableFields_v2> elementList; When I come to create the object...
3
3496
by: bassman2112 | last post by:
I'm having issues with calling a method defined in a subclass on a superclass object. This program is an exercise using inheritance, with an Employee superclass, Salaried and Hourly classes that...
1
3957
by: jimismint | last post by:
Hi guys, i'm stuck on this problem. only just recently started coding java using blue J. Can you guys help me. import java.util.*;...
0
7115
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
6978
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
7190
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...
1
6858
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...
1
4881
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4578
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...
0
3086
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
280
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.