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.cal l(a);
}
Derived.prototy pe = new Base();
Derived.prototy pe.constructor = Derived;
var derived = new Derived("hello" );
//a should be "hello" now, but it is undefined:
window.alert(de rived.a) 4 3900
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.cal l(a); }
Derived.prototy pe = new Base(); Derived.prototy pe.constructor = Derived;
var derived = new Derived("hello" ); //a should be "hello" now, but it is undefined: window.alert(de rived.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);
ingoweiss wrote:
[snip] function Derived(a) { this.prototype. constructor.cal l(a); }
[/snip]
[snip] Derived.prototy pe.constructor = Derived;
[/snip]
The last line of code sets the value of the "constructo r" property of
"Derived.protot ype" as a reference to "Derived".
So in your expression "this.prototype .constructor.ca ll", you calling
Derived, not Base.
Regards
Julian Turner
Julian Turner wrote: ingoweiss wrote: [snip] function Derived(a) { this.prototype. constructor.cal l(a); } [/snip]
[snip] Derived.prototy pe.constructor = Derived; [/snip]
The last line of code sets the value of the "constructo r" property of "Derived.protot ype" as a reference to "Derived".
So in your expression "this.prototype .constructor.ca ll", 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.
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 "constructo r" 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.construct or.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.superClass Constructor.cal l(this,a) */
/* or more simply ... */
this.superClass Constructor(a);
}
Derived.prototy pe=new Base();
Derived.prototy pe.constructor= Derived;
Derived.prototy pe.superClassCo nstructor=Base;
Regards
Julian Turner This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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
========================
import class_name as cn
obj = cn.B()
obj.printclass()
========================
|
by: bonono |
last post by:
Hi,
Suppose my class definition is like this :
class A:
name = "A"
@classmethod
def foo(cls):
cls.__super.foo()
|
by: ToChina |
last post by:
Hi, I have the following code:
class A
{
}
class B : A
{
}
|
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 supporting files
(graphics, etc) that are stored inside the package. The problem is that
the superclass's definition (stored elsewhere) has all of the code for
actually opening the files, so when I use the
os.path.dirname(os.path.abspath(__file__))...
| |
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 constructor..
class cal1
{
private int a,b,c;
public cal1(int i,int j)
{
a=i;
|
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 element for the Map I want to be able to use a subclass of the superclass for the object; for example:-
this.elementList = new HashMap<Integer, TableFields_v2_Year>();
Where TableFields_v2_Year is a subclass of TableFields_v2
However, Java won't...
|
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 extend the superclass and a Temp class that extends the Hourly class. From what I've read, this shouldn't be happening, since the object in employees is a Salaried object.
The input is a text file with every line being some piece of information, in the...
|
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.*;
------------------------------------------------------------------------------------------
public class Player
{
// the Player class has four fields
private int goals;
private int height;
private String name;
private int dateofbirth;
|
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: 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: 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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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: 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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |