473,584 Members | 2,874 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.c lass2.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.c lass2.variable) ;
}

Thanks for any advise.

//jim
Jul 23 '05 #1
2 2161
Jim Red <ji******@aol.c om> 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.c lass2.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.c lass2.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/rasterTriangleD OM.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.constructo r gives 'class1'

Danny
On Mon, 27 Jun 2005 06:30:28 -0700, Jim Red <ji******@aol.c om> 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.c lass2.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.c lass2.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
1547
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 if this doesn't see correctly with non-HTML viewers) Ok, I'm fed up with this so I'll explain the situation here and my approach (which sucks),...
4
3398
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 42. They say that 42 is printed the second time since the value was wrapped in a class and therefore became passed by reference. (sorry for any...
4
1530
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 an assembly A which refers to class Y in assembly B. So I add a reference in assembly A to assembly B, and attempt to build. I get an error message,...
3
4205
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 that needs the class module I get an error on web site: Object reference not set to an instance of an object Here is where the error is:
11
1908
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 one I select? Thanks
12
3011
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 function. When I try and compile the DLL I get "The type or namespace name "MyForm" could not be found. I think I have to reference the class but...
9
2948
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 in namespace BaseClass. Then I have a class in namespace InheritClassUser that uses the class in namespace InheritClass. The references/using are...
8
9131
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& a):m_a(a){}
12
3003
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. The global variables and global functions are hidden to prevent from accessing by the programmers. All global functions share global variables....
0
7897
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, 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...
0
7829
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8189
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, 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. ...
1
7940
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6587
agi2029
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...
0
5379
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...
1
2336
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
1
1438
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1162
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.