...

View Full Version : Is there a way to get the name of a variable?



Krupski
07-18-2011, 01:09 PM
Hi all,

I need to get the name of a Javascript variable. For example, it would work like this:



var test = document.getElementById('whatever');
var name = getName(test);
alert('The variable name is ' + name);
Running this code would result in an alert box that says "The variable name is test".

What I need to know is how to implement the "getName()" function.

Thanks!

-- Roger

devnull69
07-18-2011, 03:34 PM
Sense????

If you write getName(test) you can as well write


var name = 'test';

and you have the name of the variable test

Krupski
07-18-2011, 03:54 PM
Sense????

If you write getName(test) you can as well write


var name = 'test';
and you have the name of the variable test

You forgot the smiley after your post.

devnull69
07-18-2011, 04:14 PM
+ :-)

rnd me
07-18-2011, 09:05 PM
i don't understand why others posted jokes, this is a serious question...
Reflectivity is one area where you need to get creative using javascript since it doesn't have much built-in.


function getName(x){
for(var i in this){
if(x=== this[i]){return i;}
}//next
}//end getName()


var test = document.body;
var name = getName(test);
alert('The variable name is ' + name);

note that with primitives (string, number, and booleans), getName() might yield false matches, but it still will tell you what the var's name COULD be.
example:


function getName(x){
for(var i in this){
if(x=== this[i]){return i;}
}//next
}//end getName()



var test = {a:1, b:2, c:3, d:3};

var name = getName.call(test, test.d);
alert('The variable name is ' + name);

devnull69
07-18-2011, 09:22 PM
Sorry, but I don't get that ... can you give an example for when you can not replace getName(test) with just 'test'?

You'll have to write the variable name in your code anyway, so you can just write the name as a string literal.

rnd me
07-18-2011, 09:46 PM
Sorry, but I don't get that ... can you give an example for when you can not replace getName(test) with just 'test'?

things can have more than one name in js, especially byRef vars like objects and functions...


function getName(x){
for(var i in this){
if(x=== this[i]){return i;}
}//next
}//end getName()


function sum(a,b){return a+b || 0;};

var test = {a:1, b:2, c:sum, d:3};

var name = getName.call(test, sum);
alert('The variable name is ' + name);



or


function getName(x){
for(var i in this){
if(x=== this[i]){return i;}
}//next
}//end getName()


var o={a:1, b:2, c:3, d:3};

var test = [o.a,o.b,o.c,o.d];

var anon=test[Math.floor(test.length * Math.random())]

var name = getName.call(o, anon);
alert('The variable name is ' + name);


additionally, it's easy to imaging having to sniff event handlers and other common bound methods, without having the luxury of early definition.
well, it's easy for me to imagine; i guess if you only work with your own code in isolation you can hard-code all your names...

devnull69
07-18-2011, 09:58 PM
Ok, but I bet this is far away from what the OP had in mind. Still you gave a correct answer :-)

Krupski
07-19-2011, 03:37 AM
i don't understand why others posted jokes, this is a serious question...
Reflectivity is one area where you need to get creative using javascript since it doesn't have much built-in.


Thank you. It was indeed a serious question. What I have is a small piece of code that I use to examine everything attached to a particular element. For example, if I get the element of an image on my website with document.getElementById('logo'), I can then pass it to my code and see something like this:



e.querySelector type: function, code: function querySelector() {\n [native code]\n }
e.querySelectorAll type: function, code: function querySelectorAll() {\n [native code]\n }
e.scrollTop type: number, value: 0
e.scrollLeft type: number, value: 0
e.scrollHeight type: number, value: 69
e.scrollWidth type: number, value: 102
e.clientTop type: number, value: 0
e.clientLeft type: number, value: 0
e.clientHeight type: number, value: 69
e.clientWidth type: number, value: 102
e.firstElementChild type: object, value: [object HTMLImageElement]
e.lastElementChild type: object, value: [object HTMLImageElement]
e.previousElementSibling type: object, value: null
e.nextElementSibling type: object, value: [object HTMLHeadingElement]
e.childElementCount type: number, value: 1
e.children type: object, value: [object HTMLCollection]
........ more........
However, the "e" is a text character that I have to pass to the function.... to get the above listing I do this:



var e = document.getElementById('logo');
showElem(e, 'e');
What I want to do is just pass the e element and have the code generate the 'e' text.

The reason is that I may pass more than just a single letter. I might do this:



var e = document.getElementById('logo');
showElem(e.style, 'e.style');
...which results in this:



e.style.azimuth type: string, value: ''
e.style.background type: string, value: ''
e.style.backgroundAttachment type: string, value: ''
e.style.backgroundColor type: string, value: ''
e.style.backgroundImage type: string, value: ''
e.style.backgroundPosition type: string, value: ''
e.style.backgroundRepeat type: string, value: ''
e.style.border type: string, value: ''
e.style.borderCollapse type: string, value: ''
e.style.borderColor type: string, value: ''...... more ........

Hope this makes sense....

-- Roger



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum