Getting var name in constructor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew Poulos

    Getting var name in constructor

    If I do something like this:

    Cnst = function() {
    // blah
    }
    Cnst.prototype. meth = function() {
    // blah
    }
    var foo = new Cnst()

    how can I get the name of the var (in this case 'foo'), or at least some
    reference to it, when I'm within one of the constructor's prototypes?
    Something like:

    Cnst.prototype. meth = function() {
    this.var = ???
    }


    Andrew Poulos

  • Duncan Booth

    #2
    Re: Getting var name in constructor

    Andrew Poulos wrote:
    [color=blue]
    > If I do something like this:
    >
    > Cnst = function() {
    > // blah
    > }
    > Cnst.prototype. meth = function() {
    > // blah
    > }
    > var foo = new Cnst()
    >
    > how can I get the name of the var (in this case 'foo'), or at least some
    > reference to it, when I'm within one of the constructor's prototypes?
    > Something like:
    >
    > Cnst.prototype. meth = function() {
    > this.var = ???
    > }[/color]

    Pass it to the constructor as a parameter and save it:

    var foo = new Cnst('foo');

    In general there may not be a variable at all, or there could be several,
    so there isn't any mechanism for determining which variables refer to an
    object short of testing the values of each of the possible variables.

    Comment

    • Andrew Poulos

      #3
      Re: Getting var name in constructor

      Duncan Booth wrote:[color=blue]
      > Andrew Poulos wrote:
      >
      >[color=green]
      >>If I do something like this:
      >>
      >>Cnst = function() {
      >> // blah
      >>}
      >>Cnst.prototyp e.meth = function() {
      >> // blah
      >>}
      >>var foo = new Cnst()
      >>
      >>how can I get the name of the var (in this case 'foo'), or at least some
      >>reference to it, when I'm within one of the constructor's prototypes?
      >>Something like:
      >>
      >>Cnst.prototyp e.meth = function() {
      >> this.var = ???
      >>}[/color]
      >
      > Pass it to the constructor as a parameter and save it:
      >
      > var foo = new Cnst('foo');
      >
      > In general there may not be a variable at all, or there could be several,
      > so there isn't any mechanism for determining which variables refer to an
      > object short of testing the values of each of the possible variables.[/color]

      I was kind of hoping that I didn't have to add a parameter for
      something, I feel, the constructor/method would already know.

      At any one time isn't there only one specific instance(?) that calls a
      method?

      Andrew Poulos

      Comment

      • Duncan Booth

        #4
        Re: Getting var name in constructor

        Andrew Poulos wrote:
        [color=blue][color=green]
        >> Pass it to the constructor as a parameter and save it:
        >>
        >> var foo = new Cnst('foo');
        >>
        >> In general there may not be a variable at all, or there could be
        >> several, so there isn't any mechanism for determining which variables
        >> refer to an object short of testing the values of each of the
        >> possible variables.[/color]
        >
        > I was kind of hoping that I didn't have to add a parameter for
        > something, I feel, the constructor/method would already know.
        >
        > At any one time isn't there only one specific instance(?) that calls a
        > method?
        >[/color]

        Yes, but an instance is an object, and a variable is simply something that
        refers to an object. Think of a variable as a yellow sticky not with an
        arrow coming out of it. Assigning a variable means you put the sticky note
        so it points at the object, but there isn't anything on the object which
        points back.

        If you do this, then foo and bar are the same object, and methods just get
        the object passed to them, not the variable:

        var foo = new Cnst();
        var bar = foo;
        var baz = [foo, foo, foo];

        bar.meth();
        foo.meth();
        baz[2].meth();

        Once inside meth all of these calls are identical.

        Also, how about:

        alert('look no variables! '+ new Cnst().meth());

        Comment

        • Matt Kruse

          #5
          Re: Getting var name in constructor

          Andrew Poulos wrote:[color=blue]
          > how can I get the name of the var (in this case 'foo'), or at least
          > some reference to it, when I'm within one of the constructor's
          > prototypes?[/color]

          The important question is - why would you want to?

          Variable names are not important. They are lost when the code is compiled
          anyway.

          If you want a reference inside your object to point back to itself, keep an
          array of those objects globally, and inside the constructor set a value to
          point to which index the instance is.

          --
          Matt Kruse



          Comment

          Working...