...

View Full Version : How to get the varible name of a JS Object



apostole
08-18-2005, 07:56 PM
Yes a newbie question. Yes did some searching and could not find my answer.

If I create a JS Object like this


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


setTimeout("ps.slide()",200)

doing it the following way gives stack overflows.


setTimeout(this.slide(), 200)

martin_narg
08-18-2005, 08:38 PM
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.


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

brothercake
08-19-2005, 03:29 AM
As long as you declare "ps" in the global scope, then set it in the object constructor, you'll be good:


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:


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

rm-f
08-19-2005, 07:39 PM
I am looking for the solution as well. Unfortunately brothercake's solution does not work in my case.

The work around is:


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.

martin_narg
08-19-2005, 08:01 PM
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

dumpfi
08-19-2005, 08:52 PM
You could do something like this:


function myObject(varName) {
this.name = varName;
window[varName] = this;
this.sayHello = function() {
alert("Hello, my name is " + this.name + "!");
}
}
myObject("ps");
ps.sayHello();
dumpfi

rm-f
08-19-2005, 09:13 PM
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

martin_narg
08-19-2005, 09:39 PM
cross-browser. myBook still referred to as a global variable.



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

rm-f
08-19-2005, 11:52 PM
The problem I have is how to have an event-member function inside the dynamically created table?


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

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

apostole
08-22-2005, 08:43 PM
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?

martin_narg
08-22-2005, 08:54 PM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum