Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New to the CF scene
    Join Date
    Aug 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to get the varible name of a JS Object

    Yes a newbie question. Yes did some searching and could not find my answer.

    If I create a JS Object like this

    Code:
    var ps = new MyObject();
    How can I get the name "ps" from inside a MyObject function? The creation of the Object is in my HTML. But I need to know what it was called to do a

    Code:
    setTimeout("ps.slide()",200)
    doing it the following way gives stack overflows.

    Code:
    setTimeout(this.slide(), 200)

  • #2
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    I did it like this, but i'm sure it's not the best way - loop through the window object and do it that way - I couldn't get the reference through the object constructor.
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    <script type="text/javascript">
    
    function Book(title, author) {
    	this.title = title;
    	this.author = author;
    }
    
    Book.prototype.getName = function() {
    	for(var i in window) {
    		if(window[i] == this) {
    			return i;
    		}
    	}
    }
    
    window.onload = function() {
    	myBook = new Book("Penguin Painting for Beginners", "martin_narg");
    	alert(myBook.getName());
    }
    </script>
    </head>
    
    <body>
    </body>
    </html>
    Hope this helps

    m_n
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    As long as you declare "ps" in the global scope, then set it in the object constructor, you'll be good:
    Code:
    var ps;
    
    function MyObject()
    {
        ps = this;
    };
    Now, the constructor call can appear anywhere, in any scope, and its reference will be still be global, eg:
    Code:
    window.onload = function()
    {
        ps = new MyObject();
    };
    But of course, by referring to an object using its instantiated name, you restrict yourself to a single instance of that object. So ... the thing to do is have a sort of "caretaker" object, created in this way, which handles anything that needs to be global, such as timers or script-wide utility functions. There's no problem using its instantiated name, because there'll only ever be one.

    Then for whatever it is you want multiple instances of it, you can build them as normal, self-contained objects, and they don't need a global reference, because everything you're doing globally is in the caretaker function.

    Make sense?
    Last edited by brothercake; 08-19-2005 at 02:36 AM.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #4
    Regular Coder
    Join Date
    Aug 2005
    Location
    Toronto, ON, Canada
    Posts
    231
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am looking for the solution as well. Unfortunately brothercake's solution does not work in my case.

    The work around is:
    Code:
    function myObject_getName() {
       return this.objName;
    }
    function myObject(objectName) {
       this.objName = objectName;
    
       this.getName = myObject_getName;
    }
    
    var obj1 = new myObject('obj1');
    var obj2 = new meObject('obj2');
    
    alert(
       'obj1 name=' + obj1.objName + '\n' +
       'obj1 name=' + obj1.getName() + '\n' +
       'obj2 name=' + obj2.objName + '\n' +
       'obj2 name=' + obj2.getName() + '\n'
    );
    I hoppe somebody has a better solution.

  • #5
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Does my solution not work for you? It returns the object name without ever storing it within the object itself. It's not very elegant but it works well without statically declaring the variable anytime other than creating an instance of the object.

    m_n
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper

  • #6
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    You could do something like this:
    Code:
    function myObject(varName) {
      this.name = varName;
      window[varName] = this;
      this.sayHello = function() {
        alert("Hello, my name is " + this.name + "!");
      }
    }
    myObject("ps");
    ps.sayHello();
    dumpfi

  • #7
    Regular Coder
    Join Date
    Aug 2005
    Location
    Toronto, ON, Canada
    Posts
    231
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry Martin,

    The solution you have posted is The One (except browsers compatibility).

    I run the script in IE6 and got 'undefined' msg, so I just drift away. On your second post I run the script in MOZ5/FF1.0.6 and found the solution is 'what I was looking for'. Thanks for help.

    rm -f

  • #8
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    cross-browser. myBook still referred to as a global variable.

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    <script type="text/javascript">
    
    function Book(title, author) {
    	this.title = title;
    	this.author = author;
    }
    
    Book.prototype.getName = function() {
    	for(var i in window) {
    		if(window[i] == this) {
    			return i;
    		}
    	}
    }
    
    window.onload = function() {
    	window["myBook"] = new Book("Penguin Painting for Beginners", "martin_narg");
    	alert(myBook.getName());
    }
    </script>
    </head>
    
    <body>
    </body>
    </html>
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper

  • #9
    Regular Coder
    Join Date
    Aug 2005
    Location
    Toronto, ON, Canada
    Posts
    231
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The problem I have is how to have an event-member function inside the dynamically created table?

    Code:
    does not work:
    <input type=text onkeydown=\"MTable.onKeyD(); return false\">';
    
    does not work:
    <input type=text onkeydown=\"this.onKeyD(); return false\">';
    
    works only for mT1 table:
    <input type=text onkeydown=\"mT1.onKeyD(); return false\">';

    The Event needs to be processed inside the member function, as I need to use member variables (this.cols, for example).

    Page Code
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    <script type="text/javascript">
    
    /*class MTable - start */
    MTable.prototype.getHtmlString = function() {
    	var s = '';
    	s += '<form>';
    	s += '<table border=1>';
    	s += '  <tr>';
    	for(var i = 0; i<this.cols; i++) {
    	  s += '    <td>';
    	  s += '      <input type=text onkeydown=\"mT1.onKeyD(); return false\">';
    	  s += '    </td>';
    	};
    	s += '  </tr>';
    	s += '</table>';
    	s += '</form>';
    	return s;
    }
    
    MTable.prototype.onKeyD = function() {
    	alert(
    		'cols=' + this.cols + '\n'
    	);
    }
    
    function MTable(cols) {
    	this.cols = cols;
    }
    /*class MTable - end */
    
    </script>
    </head>
    
    <body>
    <script language="javascript">
    	var mT1 = new MTable(3);
        var mT2 = new MTable(5);
        document.write(mT1.getHtmlString());
        document.write('<br>');
        document.write(mT2.getHtmlString());
    </script>
    </body>
    </html>

  • #10
    New to the CF scene
    Join Date
    Aug 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Not what I was looking for

    The Object being created is in an external .js file. The object is being instatated in the small script section of the HTML file.


    I was hoping for an answer like reflection in Java -- apparently not the case in JavaScript. Is there not way to query an object about its name without priming some attribute with the name first?

  • #11
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    As I did, you can go to the object's parent and loop through, checking to see if the object exists in it using a for in loop.

    Hope this helps

    m_n
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper


  •  

    Posting Permissions

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