reference a 'class' in a 'class'

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim Red

    #1

    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
  • Lasse Reichstein Nielsen

    #2
    Re: reference a 'class' in a 'class'

    Jim Red <jimred73@aol.c om> writes:
    [color=blue]
    > first of all, i know, there are no classes in javascript. but i will
    > use that word for better understanding of my question.[/color]

    Let's see if it works :)
    [color=blue]
    > here we go. i have three classes and need a reference to the parent class.[/color]

    What is a parent class? (so, no it didn't work)
    [color=blue]
    > could this be done by a reference, or just by constructing the class.[/color]

    Referencing what? Or constructing an *object* that is an instance of
    which class?
    [color=blue]
    > 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) ;[/color]

    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" :)
    [color=blue]
    > }
    >
    > var class1 = new Class1();
    >
    > in flash, we could use'_parent'.[/color]

    For what? (I'm not familiar with Flash)
    [color=blue]
    > is there another way to get a reference to the class1 in javascript?[/color]

    No. There is no other reference than the one kept in the variable
    "class1". You have not created any other references.
    [color=blue]
    > 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);[/color]

    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.
    [color=blue]
    > }
    >
    > function Class2(_parent) {[/color]

    Add:
    this._parent = _parent;

    if you want to preserve the reference.
    [color=blue]
    > this.variable = "hello world";
    > }
    >
    >
    > function Class3(_parent) {
    > alert(_parent.c lass2.variable) ;[/color]

    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 - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Danny

      #3
      Re: reference a 'class' in a 'class'


      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 <jimred73@aol.c om> wrote:
      [color=blue]
      > 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[/color]



      --
      Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

      Comment

      Working...