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.


    Philip, Jan 6, 2004
    1. Advertisements

  2. 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?
    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.
    No, Javascript has static scope. You can't leave the scope you start
    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?

    Lasse Reichstein Nielsen, Jan 6, 2004
    1. Advertisements

  3. Been having a really tricky problem. I'm trying to do some object oriented
    Can we see more of the actual program? This abstract looks ok.
    Douglas Crockford, Jan 7, 2004
  4. Philip

    Philip Guest

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

    var Global = new Ob("If this shows, it worked!");
    <a href="#" onclick="Global.Method();return
    Philip, Jan 7, 2004
  5. Always a good thing to do!
    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;
    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?

    Lasse Reichstein Nielsen, Jan 7, 2004
  6. What to notice in the code... When running this
    Your pattern looks clean. What happens when Global.Method is call normally (not
    in an event handler)?
    Douglas Crockford, Jan 8, 2004
  7. Philip

    Philip Guest

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

    Philip, Jan 8, 2004
  8. JavaScript 1.3 *claims* to be "fully compatible" to the *first* edition
    of ECMAScript, see


    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


    Thomas 'PointedEars' Lahn, Jan 20, 2004
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.