SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru
    Join Date
    Nov 2005
    Location
    Midwest
    Posts
    777
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    OO JavaScript Get Reference Name

    Hello

    I am curious if it is possible for an object in JavaScript to know what its reference name is without passing it in as a parameter?

    Thanks in advance for your time.

    Here is an example.

    Code:
    <html>
    <head>
    
    <script language="JavaScript">
    
    function test()
    {
       var testRef = new classExample();
    }
    
    function classExample()
    {
       //Is there a way this class can determine the reference name?
       //In this example, the reference name is testRef?
    }
    
    </script>
    </head>
    
    <body>
    
    <a href="javascript: test()">Test</a>
    
    </body>
    </html>

  2. #2
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, there isn't. Why would you want to do that anyway?

  3. #3
    SitePoint Guru
    Join Date
    Nov 2005
    Location
    Midwest
    Posts
    777
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have multiple JavaScript classes and I am using setTimeout and the problem with setTimeout is that I have to pass it a String instead of the reference to the class object. So if I say this. with setTimeout but call it from a different class, this refers to the wrong class. If I knew the reference name, I could use the reference name instead of this.

  4. #4
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can't you do something like this?
    Code JavaScript:
    function test() {
        var testRef = new ClassExample();
        window.setTimeout(testRef.doSomething, 2000);
    }
     
    function ClassExample() {
        // constructor
    }
     
    ClassExample.prototype.doSomething = function () {
        alert("Hello, World!");
    };
    Birnam wood is come to Dunsinane

  5. #5
    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)
    Quote Originally Posted by TryingToLearn View Post
    ... and the problem with setTimeout is that I have to pass it a String
    No, you can pass a function too if you want.
    Code JavaScript:
    function doSomething(){
        //
    }
     
    window.setTimeout(doSomething, 1000);
    The function you pass can also be anonymous:
    Code JavaScript:
    window.setTimeout(function() {
        alert("Hi!");
    }, 1000);
    The keyword this will point to the window object, so you need to create a variable to point to any of your JavaScript objects:
    Code JavaScript:
    // constructor
    function Person(name) {
        this.name = name;
    }
     
    // say hi after 1 second
    Person.prototype.sayHi = function() {
        var oThis = this;
     
        window.setTimeout(function() {
            alert(oThis.name);
        }, 1000);
    }
     
    var dude = new Person("Toby");
    dude.sayHi();

  6. #6
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or, if you need to pass arguments to the function,
    Code JavaScript:
    function test() {
        var testRef = new ClassExample();
        window.setTimeout(function () {testRef.doSomething(42);}, 2000);
    }
     
    function ClassExample() {
        // constructor
    }
     
    ClassExample.prototype.doSomething = function (n) {
        alert(n);
    };
    Birnam wood is come to Dunsinane

  7. #7
    SitePoint Guru
    Join Date
    Nov 2005
    Location
    Midwest
    Posts
    777
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for all of the feedback... it has been helpful.


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
  •