reference a 'class' in a 'class'

J

Jim Red

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
 
L

Lasse Reichstein Nielsen

Jim Red said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top