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

reference a 'class' in a 'class'

hello

first of all, i know, there are no classes in javascript. but i will use
that word for better understanding of my question.

here we go. i have three classes and need a reference to the parent class.
could this be done by a reference, or just by constructing the class.

function Class1() {
this.class2 = new Class2();
this.class3 = new Class3();
}

function Class2() {
this.variable = "hello world";
}
function Class3() {
// just for this example. is not working
alert(_parent.class2.variable);
}

var class1 = new Class1();

in flash, we could use'_parent'. is there another way to get a reference
to the class1 in javascript?

Of course, i can pass the this pointer. But I'm wondering if theres
another possibility.

function Class1() {
this.class2 = new Class2(this);
this.class3 = new Class3(this);
}

function Class2(_parent) {
this.variable = "hello world";
}
function Class3(_parent) {
alert(_parent.class2.variable);
}

Thanks for any advise.

//jim
Jul 23 '05 #1
2 2148
Jim Red <ji******@aol.com> writes:
first of all, i know, there are no classes in javascript. but i will
use that word for better understanding of my question.
Let's see if it works :)
here we go. i have three classes and need a reference to the parent class.
What is a parent class? (so, no it didn't work)
could this be done by a reference, or just by constructing the class.
Referencing what? Or constructing an *object* that is an instance of
which class?
function Class1() {
this.class2 = new Class2();
this.class3 = new Class3();
}

function Class2() {
this.variable = "hello world";
}
function Class3() {
// just for this example. is not working
alert(_parent.class2.variable);
There is no relation between Class1 and Class3 except that objects
created using Class1() contains a reference to an object created
from Class3(). But that's just a reference. You could have lots
of references to that object. What if you did:

var c1 = new Class1();
var c2 = new Class2();
c2.class3 = c1.class3;

What is the "parent" of c1.class3 then?
Try to express, concisely, what you want to be able to do, preferably
without using the word "class" :)
}

var class1 = new Class1();

in flash, we could use'_parent'.
For what? (I'm not familiar with Flash)
is there another way to get a reference to the class1 in javascript?
No. There is no other reference than the one kept in the variable
"class1". You have not created any other references.
Of course, i can pass the this pointer. But I'm wondering if theres
another possibility.

function Class1() {
this.class2 = new Class2(this);
this.class3 = new Class3(this);
That would allow the instances of Class2 and Class3 to know of the
instance of Class1 that they are created at the same time as.
It is a reasonable solution.
}

function Class2(_parent) {
Add:
this._parent = _parent;

if you want to preserve the reference.
this.variable = "hello world";
}
function Class3(_parent) {
alert(_parent.class2.variable);


Here you are lucky that you initialize the "class2" property before
you create the instance of Class3. The other way around wouldn't work.

If the Class3 constructor needs the reference to the Class2 instance,
you might pass that directly, instead of passing a reference to the
Class1 instance.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #2

Semantically, there aren't any per se, not quite really, Practically,
there are in js, custom contructors are pretty much that, and the way to
refer to the constructor for the object is by the .constructor property of
it:

duck=new class1(); duck.constructor gives 'class1'

Danny
On Mon, 27 Jun 2005 06:30:28 -0700, Jim Red <ji******@aol.com> wrote:
hello

first of all, i know, there are no classes in javascript. but i will use
that word for better understanding of my question.

here we go. i have three classes and need a reference to the parent
class.
could this be done by a reference, or just by constructing the class.

function Class1() {
this.class2 = new Class2();
this.class3 = new Class3();
}

function Class2() {
this.variable = "hello world";
}
function Class3() {
// just for this example. is not working
alert(_parent.class2.variable);
}

var class1 = new Class1();

in flash, we could use'_parent'. is there another way to get a reference
to the class1 in javascript?

Of course, i can pass the this pointer. But I'm wondering if theres
another possibility.

function Class1() {
this.class2 = new Class2(this);
this.class3 = new Class3(this);
}

function Class2(_parent) {
this.variable = "hello world";
}
function Class3(_parent) {
alert(_parent.class2.variable);
}

Thanks for any advise.

//jim


--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Jul 23 '05 #3

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

Similar topics

5
by: Javier Campos | last post by:
WARNING: This is an HTML post, for the sake of readability, if your client can see HTML posts, do it, it doesn't contain any script or virus :-) I can reformat a non-HTML post if you want me to (and...
4
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then...
4
by: Edward Diener | last post by:
I have a class Y in assembly B which is derived from a class Z in assembly C. So I correctly add a reference to assembly C in assembly B, build assembly B and everything builds fine. Now I create...
3
by: Adam | last post by:
We have a web site that uses .vb for the web pages and .cs for a class module. We are getting the error in .NET 2.0 and VS 2005 beta 2. It does work with .NET 1.1. When trying to access a page...
11
by: Just Me | last post by:
I have a solution containing many usercontrol projects. When I wish to reference a usercontrol in another project I can select either the project or the assembly. Does it make a difference which...
12
by: scottt | last post by:
hi, I am having a little problem passing in reference of my calling class (in my ..exe)into a DLL. Both programs are C# and what I am trying to do is pass a reference to my one class into a DLL...
9
by: MSDNAndi | last post by:
Hi, I have the following problem with VS 2005 (Professional) and C# 2.0 I will use assembly and namespace synonymously here. I have a class in namespace InheritClass inherit from a baseclass...
8
by: Bart Simpson | last post by:
If a class has a member variable that is a reference. What happens to teh class that is being referenced, when the containing class is destroyed? e.g. Class A{ }; Class B { B(const A&...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
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
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
Oralloy
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,...
0
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...
0
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...
0
tracyyun
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...
0
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...

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.