Javascript object Self reference

Discussion in 'Javascript' started by Philip, Jan 6, 2004.

  1. Philip

    Philip Guest

    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. ()
    // ASPTools.Biz (http://www.asptools.biz)
    // ASP, ASP.NET and Javascript web applications
     
    Philip, Jan 6, 2004
    #1
    1. Advertising

  2. "Philip" <> writes:

    > 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);
    > }
    > }


    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?

    > 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.


    When the method is called, you don't use this. This is used when you
    create the object with
    new Object(propertyValue)
    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.

    > 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?


    No, Javascript has static scope. You can't leave the scope you start
    in.

    > Is this the case?


    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 -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Jan 6, 2004
    #2
    1. Advertising

  3. > 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.


    Can we see more of the actual program? This abstract looks ok.
     
    Douglas Crockford, Jan 7, 2004
    #3
  4. Philip

    Philip Guest

    "Lasse Reichstein Nielsen" <> wrote in message
    news:...
    > What was the exact code you used, and how did you use it?
    >


    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><title>Test Page</title>
    <script language=javascript>
    <!--//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();return
    false;window.event.returnValue=false;">Click</a>
    </body></html>


    --
    // Philip Dearmore ()
    // ASPTools.Biz (http://www.asptools.biz)
    // ASP, ASP.NET and Javascript web applications
     
    Philip, Jan 7, 2004
    #4
  5. "Philip" <> writes:

    > Yes, thanks to both of you for the replies. Sorry about the delay--I had to
    > boil it down and isolate the problem.


    Always a good thing to do!

    > 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".


    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 -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Jan 7, 2004
    #5
  6. > 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".


    > <html><head><title>Test Page</title>
    > <script language=javascript>


    > 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();return
    > false;window.event.returnValue=false;">Click</a>
    > </body></html>


    Your pattern looks clean. What happens when Global.Method is call normally (not
    in an event handler)?
     
    Douglas Crockford, Jan 8, 2004
    #6
  7. Philip

    Philip Guest

    "Lasse Reichstein Nielsen" <> wrote in message
    news:...
    > 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!)


    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 ()
    // ASPTools.Biz (http://www.asptools.biz)
    // ASP, ASP.NET and Javascript web applications
     
    Philip, Jan 8, 2004
    #7
  8. Lasse Reichstein Nielsen wrote:
    > (Isn't JavaScript 1.3 supposed to be ECMAScript v3 compliant? Or was it only
    > ECMAScript v2?


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

    <http://devedge.netscape.com/library/manuals/2000/javascript/1.5/guide/intro.html#1013678>

    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.netscape.com/library/manuals/2000/javascript/1.5/guide/preface.html#1003515>
    <http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/preface.html#1003515>


    PointedEars
     
    Thomas 'PointedEars' Lahn, Jan 20, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ralf W. Grosse-Kunstleve
    Replies:
    16
    Views:
    612
    Lonnie Princehouse
    Jul 11, 2005
  2. Ralf W. Grosse-Kunstleve
    Replies:
    18
    Views:
    612
    Bengt Richter
    Jul 11, 2005
  3. Ralf W. Grosse-Kunstleve
    Replies:
    2
    Views:
    420
    Dan Sommers
    Jul 12, 2005
  4. falcon
    Replies:
    0
    Views:
    396
    falcon
    Jul 31, 2005
  5. Bart Kastermans
    Replies:
    6
    Views:
    420
    Bart Kastermans
    Jul 13, 2008
Loading...

Share This Page