SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Prototypes, constructors and instance names

    Hi guys,

    I've got another question relating to OOP in Javascript.

    If I create a class, is there anyway for the constructor function to know what the instance name of the new object is, without the name being passed in as a parameter?

    I guess what I'm looking for is something that would work like:

    function myClass(){
    var objectName=this.<instanceName>;
    }

    var newObject=new myClass(); //the constructor should then store "newObject" in the objectName variable.

    Is this possible? Any ideas?

    Thanks

    NickJ.

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My first question would be, why would you like to do that?

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The reason for doing that is because I have a Class used to define one or more objects, and each of those objects needs to "refresh" itself periodically. I've been using setTimeout, and while I can pass a reference to right method into the setTimeout callback (e.g. setTimeout(this.doRefresh, delay)), when the method eventually fires, it has effectively been called by the window object, so all references to "this" in the method point to the window object and not the object being refreshed, which is pretty pointless.

    So what I want to do is use the more traditional setTimeout("string to evaluate", delay), where the string passed in includes the instance name and the method to be called. Ive currently got this working by taking in the instance name as a parameter on the constructor, but I was wondering if there is a "tidier" way of doing it.

    Really what I want to know is whether or not I can get such a reference or not.

    NickJ

  4. #4
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, what you need is something like this then:
    Code:
    // Constructor
    function TestObject()
    {
        // A variable to refer to itself
        var oThis = this;
    
        window.setTimeout(function()
        {
            oThis.doRefresh();
        }, 1000);
    }
    
    TestObject.prototype.doRefresh = function()
    {
        alert("tada!");
    }
    
    var oTest = new TestObject();

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that,

    It works like a dream.

    NickJ


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •