Javascript object Self reference

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

    Javascript object Self reference

    Hi all,

    Been having a really tricky problem. I'm trying to do some object oriented
    programming with javascript and it was all working fine until I had to
    support Netscape 4. The problem is that my object doesn't seem to be able
    to refer back to itself. Up until now, I've been using code like this...

    function Object
    {
    var Self = this;
    this.Property = Property;
    this.Method = Method;

    function Method ()
    {
    alert (Self.Property) ;
    }
    }

    This is because when the Method was called by an event handler, you couldn't
    use 'this' to refer to the object because it was referring to (I believe)
    either the event itself or the window or something. Using Self always
    worked on Netscape 6, Mozilla, Opera, IE, etc.. but with Netscape 4 it comes
    back saying that Self has no properties, leading me to believe that perhaps
    it went out of scope? Is this the case? If so, how could I remedy that? I
    need to have some kind of variable or property as part of the object that
    can refer back to the object itself.

    Thanks!

    Philip

    --
    // Philip D. (games@aspNospa minherEtools.bi z)
    // ASPTools.Biz (http://www.asptools.biz)
    // ASP, ASP.NET and Javascript web applications


  • Lasse Reichstein Nielsen

    #2
    Re: Javascript object Self reference

    "Philip" <padearmore@spe llnextright.var izun.net> writes:
    [color=blue]
    > Up until now, I've been using code like this...
    >
    > function Object
    > {
    > var Self = this;
    > this.Property = Property;
    > this.Method = Method;
    >
    > function Method ()
    > {
    > alert (Self.Property) ;
    > }
    > }[/color]

    Well, it could at most have been *like* this, because this doesn't work
    (no argument to the function - I guess it should be Property - and the
    function is called Object, which is bound to conflict with something).

    What was the exact code you used, and how did you use it?
    [color=blue]
    > This is because when the Method was called by an event handler, you couldn't
    > use 'this' to refer to the object because it was referring to (I believe)
    > either the event itself or the window or something.[/color]

    When the method is called, you don't use this. This is used when you
    create the object with
    new Object(property Value)
    Inside the body of a handler function, "this" refers to the object
    that the handler is on. When you call a method from inside the handler,
    the value of "this" is different for that method's body - it is the
    object the method is a method of. If you just call a function, not as
    a member of an object, "this" refers to the global object.
    [color=blue]
    > Using Self always worked on Netscape 6, Mozilla, Opera, IE,
    > etc.. but with Netscape 4 it comes back saying that Self has no
    > properties, leading me to believe that perhaps it went out of scope?[/color]

    No, Javascript has static scope. You can't leave the scope you start
    in.
    [color=blue]
    > Is this the case?[/color]

    Probably not, but you need to show us the actual code that has this
    problem for us to test it. Just a minimal example that show the
    problem. Also, which version of Netscape 4 do you use?

    /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

    • Douglas Crockford

      #3
      Re: Javascript object Self reference

      > Been having a really tricky problem. I'm trying to do some object oriented[color=blue]
      > programming with javascript and it was all working fine until I had to
      > support Netscape 4. The problem is that my object doesn't seem to be able
      > to refer back to itself. Up until now, I've been using code like this...
      >
      > function Object
      > {
      > var Self = this;
      > this.Property = Property;
      > this.Method = Method;
      >
      > function Method ()
      > {
      > alert (Self.Property) ;
      > }
      > }
      >
      > This is because when the Method was called by an event handler, you couldn't
      > use 'this' to refer to the object because it was referring to (I believe)
      > either the event itself or the window or something. Using Self always
      > worked on Netscape 6, Mozilla, Opera, IE, etc.. but with Netscape 4 it comes
      > back saying that Self has no properties, leading me to believe that perhaps
      > it went out of scope? Is this the case? If so, how could I remedy that? I
      > need to have some kind of variable or property as part of the object that
      > can refer back to the object itself.[/color]

      Can we see more of the actual program? This abstract looks ok.

      Comment

      • Philip

        #4
        Re: Javascript object Self reference

        "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
        news:65fobuh1.f sf@hotpop.com.. .[color=blue]
        > What was the exact code you used, and how did you use it?
        >[/color]

        Yes, thanks to both of you for the replies. Sorry about the delay--I had to
        boil it down and isolate the problem. I believe the following code
        illustrates it perfectly. What to notice in the code... When running this
        in Netscape 4.79, an error message comes up in the Javascript console saying
        that "Self has no properties".

        ** Code follows **

        <html><head><ti tle>Test Page</title>
        <script language=javasc ript>
        <!--//Netscape Communicator 4.79 Test
        function Ob( Arg )
        {
        var Self = this;
        this.Prop = Arg;
        this.Method = Method;
        alert (Self.Prop); // Shows that as of construction, Self works

        function Method()
        {
        // Following line works as 'this' but not as 'Self'.
        alert(Self.Prop );
        }
        }

        var Global = new Ob("If this shows, it worked!");
        //-->
        </script></head>
        <body>
        <a href="#" onclick="Global .Method();retur n
        false;window.ev ent.returnValue =false;">Click</a>
        </body></html>


        --
        // Philip Dearmore (pdearmore@aspt ools.biz)
        // ASPTools.Biz (http://www.asptools.biz)
        // ASP, ASP.NET and Javascript web applications


        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: Javascript object Self reference

          "Philip" <padearmore@spe llnextright.var izun.net> writes:
          [color=blue]
          > Yes, thanks to both of you for the replies. Sorry about the delay--I had to
          > boil it down and isolate the problem.[/color]

          Always a good thing to do!
          [color=blue]
          > I believe the following code illustrates it perfectly. What to
          > notice in the code... When running this in Netscape 4.79, an error
          > message comes up in the Javascript console saying that "Self has no
          > properties".[/color]

          Yes, I get the same in Netscape 4.8. It is clearly a bug.

          I reduced the example to just three lines inside Ob:
          ---
          function Ob() {
          var Self = this;
          this.Method = Methodx;
          function Methodx() {alert(Self);}
          }
          ---
          and this fails (alerts "undefined" ). Then I tried rearranging the
          lines. You can move the "var Self=this" anywhere without changing anyting.
          However, the following ones do work (alerts "[object Object])":

          ---
          function Ob() {
          var Selfx = this; // again doesn't matter where this line is
          function Methodx() {alert(Selfx);}
          this.Method = Methodx;
          }
          ---
          and
          ---
          function Ob() {
          Self = this;
          this.Method = Methodx;
          // Self = this; // or here, but not between "function" and "var"
          function Methodx() {alert(Self);}
          var Self;
          }
          ---
          So, something goes wrong in how NS4 builds its closures (which is
          fairly impressive, it's not really that hard!)

          I'm afraid I'm too tired to figure out what on earth they were thinking!
          (Isn't JavaScript 1.3 supposed to be ECMAScript v3 compliant? Or was it only
          ECMAScript v2?

          /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

          • Douglas Crockford

            #6
            Re: Javascript object Self reference

            > What to notice in the code... When running this[color=blue]
            > in Netscape 4.79, an error message comes up in the Javascript console saying
            > that "Self has no properties".[/color]
            [color=blue]
            > <html><head><ti tle>Test Page</title>
            > <script language=javasc ript>[/color]
            [color=blue]
            > function Ob( Arg )
            > {
            > var Self = this;
            > this.Prop = Arg;
            > this.Method = Method;
            > alert (Self.Prop); // Shows that as of construction, Self works
            >
            > function Method()
            > {
            > // Following line works as 'this' but not as 'Self'.
            > alert(Self.Prop );
            > }
            > }
            >
            > var Global = new Ob("If this shows, it worked!");[/color]
            [color=blue]
            > </script></head>
            > <body>
            > <a href="#" onclick="Global .Method();retur n
            > false;window.ev ent.returnValue =false;">Click</a>
            > </body></html>[/color]

            Your pattern looks clean. What happens when Global.Method is call normally (not
            in an event handler)?

            Comment

            • Philip

              #7
              Re: Javascript object Self reference

              "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
              news:llojnx7k.f sf@hotpop.com.. .[color=blue]
              > and this fails (alerts "undefined" ). Then I tried rearranging the
              > lines. You can move the "var Self=this" anywhere without changing anyting.
              > However, the following ones do work (alerts "[object Object])":
              > So, something goes wrong in how NS4 builds its closures (which is
              > fairly impressive, it's not really that hard!)[/color]

              Wow, you are a genius! Myself being the classic cookie-cutter programmer I
              would never have thought to rearrange the declarations. I've rearranged the
              code in my original objects and it is working now. I thought I was going to
              have to rewrite everything using global variables or something...

              Thanks!

              --
              // Philip Dearmore (pdearmore@aspt ools.biz)
              // ASPTools.Biz (http://www.asptools.biz)
              // ASP, ASP.NET and Javascript web applications


              Comment

              • Thomas 'PointedEars' Lahn

                #8
                Re: Javascript object Self reference

                Lasse Reichstein Nielsen wrote:[color=blue]
                > (Isn't JavaScript 1.3 supposed to be ECMAScript v3 compliant? Or was it only
                > ECMAScript v2?[/color]

                JavaScript 1.3 *claims* to be "fully compatible" to the *first* edition
                of ECMAScript, see

                <http://devedge.netscap e.com/library/manuals/2000/javascript/1.5/guide/intro.html#1013 678>

                But Netscape seems to have weird definitions of "based on" and
                "fully compatible" anyway. Take for example the unary "+" and
                "typeof" operators not supported in some NN3/4 versions despite

                <http://devedge.netscap e.com/library/manuals/2000/javascript/1.5/guide/preface.html#10 03515>
                <http://devedge.netscap e.com/library/manuals/2000/javascript/1.5/reference/preface.html#10 03515>


                PointedEars

                Comment

                Working...