473,327 Members | 2,012 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,327 software developers and data experts.

Inheritance and private variables

Hallo.

Is it possible to set private variables "var c=..." in the parent class
and use them in the inherited class?

My parent class looks like this:
function parentclass(){
var txt = 'foo';
}

My child class is this:
function childclass(){
var p = document.getElementById('myElement');
p.addEventListener('click', action, true);
function action(){ alert(txt);}
}
childclass.prototype = new parentclass();

Now the problem is: The variable txt in alert is not defined.
Well a workaround is to set the variable as public in the parent class
this.txt = foo;
and copy the variable in the child class into a private variable
var txt = this.txt;
because this.txt doesn't work in the alert-call.

Do you have any idea how to solve this? Or is my total approach wrong?

Thanks for your help in advance.

Greetings,
Björn
The full example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
</head>
<script type="text/javascript">
function parentclass(){
// this.txt = 'foo'; // Workaround
var txt = 'foo';
}

function childclass(){
// var txt = this.txt; // Workaround
var p = document.getElementById('myElement');
p.addEventListener('click', action, true);
function action(){ alert(txt);}
}
childclass.prototype = new parentclass();

function init(){var child = new childclass();}

</script>

<body onLoad="init()">
<p id="myElement">Text</p>
</body>
</html>
Oct 20 '06 #1
1 1383
you could do the following:

function childclass(){
// var txt = this.txt; // Workaround
var p = document.getElementById('myElement');
p.addEventListener('click', action, true);
var me = this;
function action(){ alert(me.txt);}
}

Now the action closure has a reference "me" to the actual instance of
childclass.

dip

On Oct 20, 8:23 am, Björn Langhof <i...@web.dewrote:
Hallo.

Is it possible to set private variables "var c=..." in the parent class
and use them in the inherited class?

My parent class looks like this:
function parentclass(){
var txt = 'foo';

}My child class is this:
function childclass(){
var p = document.getElementById('myElement');
p.addEventListener('click', action, true);
function action(){ alert(txt);}}childclass.prototype = new parentclass();

Now the problem is: The variable txt in alert is not defined.
Well a workaround is to set the variable as public in the parent class
this.txt = foo;
and copy the variable in the child class into a private variable
var txt = this.txt;
because this.txt doesn't work in the alert-call.

Do you have any idea how to solve this? Or is my total approach wrong?

Thanks for your help in advance.

Greetings,
Björn

The full example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
</head>
<script type="text/javascript">
function parentclass(){
// this.txt = 'foo'; // Workaround
var txt = 'foo';

}function childclass(){
// var txt = this.txt; // Workaround
var p = document.getElementById('myElement');
p.addEventListener('click', action, true);
function action(){ alert(txt);}}childclass.prototype = new parentclass();

function init(){var child = new childclass();}

</script>

<body onLoad="init()">
<p id="myElement">Text</p>
</body>
</html>
Oct 20 '06 #2

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

Similar topics

7
by: Asapi | last post by:
When a derived class inherits from a base class, does the former inherits everything, including public/protected/private instance data, static data, and various methods(static, private/public,...
36
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...
14
by: dl | last post by:
I have two classes, say A and B, both having a data member 'int n'; private in A, public in B. When I derive class C from both public A and public B, B::n should be visible to C while A::n...
2
by: beseecher | last post by:
Hi, In my research in the javascript language I have encountered problems with implementing prototype inheritance while preserving private methods functioning properly. Here is an example: ...
5
by: tshad | last post by:
In VS 2003, I am setting up an abstract class that is setting up classes for each datatype of VB.Net (as well as C#). I am trying to set it up so that most of the work is done in the Abstract...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.