Hi there:
Is it possible to probe the name of the variable assigned to an object instance in JavaScript? For example:
Code:
function house(){
}
var bobhouse=new house()
if (instancename=="bobhouse")
//do something
In other words, can I do some sort of string comparison on the variable name "bobname", so I know it's actually called "bobname"? I feel I'm asking how to have a variable recognize itself as a variable.
__________________
- George
- JavaScript Kit- JavaScript tutorials and 400+ scripts!
- JavaScript Reference- JavaScript reference you can relate to.
Maybe you can alter the prototype of a function object to get it to do what you want to? That might be similar to what I did. I don't think there's a "built-in" way for JS to do what you want.
functions have a .name property, but onlew function objects.
you will have to sweep window to find the name of your non-function variable.
the example below works for firefox, not sure about other browsers.
Code:
function obSub(ob) {var r = [];var i = 0;for (var z in ob) {
if (ob.hasOwnProperty(z)) {r[i++] = z;}}return r;}
function getVarName(variable){
return obSub(window).map(function(a){
if( window[a] === variable ){return a} }).sort()[0]
}
function House(){ }
var tre = new House()
alert( getVarName(tre) )
__________________ my site (updated 5/13) STATS (2013/5) HTML5:90.2% MOB:15.2% IE7:0.5% IE8:8.4% IE9:8.5% IE10:8.5%
Thanks md_me. Haven't tested your code in IE yet, but if that's what it requires in terms of complexity, I think I'll just choose an alternate approach to my problem. Thx though.
__________________
- George
- JavaScript Kit- JavaScript tutorials and 400+ scripts!
- JavaScript Reference- JavaScript reference you can relate to.
functions have a .name property, but onlew function objects.
you will have to sweep window to find the name of your non-function variable.
the example below works for firefox, not sure about other browsers.
Code:
function obSub(ob) {var r = [];var i = 0;for (var z in ob) {
if (ob.hasOwnProperty(z)) {r[i++] = z;}}return r;}
function getVarName(variable){
return obSub(window).map(function(a){
if( window[a] === variable ){return a} }).sort()[0]
}
function House(){ }
var tre = new House()
alert( getVarName(tre) )
I get the code here, but my question to WA is: If you're dealing with a bunch of "house" object instances, isn't manually creating a "name" property for the "house" function object easier?
I get the code here, but my question to WA is: If you're dealing with a bunch of "house" object instances, isn't manually creating a "name" property for the "house" function object easier?
you cannot alter the name property of a function object.
at least not in firefox:
Code:
function test(){
return true
}
alert(test.name) // shows test
test.name = "newname"
alert(test.name) // shows test
if there is not a name used (anonymous), as in :
Code:
var test = function (){
return true
}
alert(test.name) // shows ""
test.name = "newname"
alert(test.name) // shows ""
it still doesnt work...
OP:
you might consider declaring your own top level object like App ={} at the top of your script, and assign properties to it as you write your other code.
ex: App.goodNumbers=[1,2,3,4]
later : alert(App.goodNumbers);
it is easier and safer to iterate that App object than the window object, and can be done across browsers relatively simply.
__________________ my site (updated 5/13) STATS (2013/5) HTML5:90.2% MOB:15.2% IE7:0.5% IE8:8.4% IE9:8.5% IE10:8.5%
you cannot alter the name property of a function object.
I'm sorry, I didn;t mean literally name the property "name"...any property named something else (other than "name") that serves the purpose of identifying a given function object instance, i meant...
Per the example in my first post to this thread...
In other words, can I do some sort of string comparison on the variable name "bobname", so I know it's actually called "bobname"? I feel I'm asking how to have a variable recognize itself as a variable.
What would happen if there are multiple variables referencing the same object?
If anything, unless I'm horribly wrong, I would suspect that there is a much better way of solving a problem than this. Variables shouldn't need to know about other variables . . . that's your job (from a lexical standpoint). Variables just need to know values.
Variables shouldn't need to know about other variables . . . that's your job (from a lexical standpoint). Variables just need to know values.
Perhaps, and it's really more of a curiosity than necessarily to do this for me at this point. Broadening the question though, is there a way for a variable to know what it itself is called? Something like:
Code:
var x=5
if (nameOf(x)=="x")
//do something
__________________
- George
- JavaScript Kit- JavaScript tutorials and 400+ scripts!
- JavaScript Reference- JavaScript reference you can relate to.
Hi there:
Is it possible to probe the name of the variable assigned to an object instance in JavaScript? For example:
Code:
function house(){
}
var bobhouse=new house()
if (instancename=="bobhouse")
//do something
In other words, can I do some sort of string comparison on the variable name "bobname", so I know it's actually called "bobname"? I feel I'm asking how to have a variable recognize itself as a variable.
You could try something like this:
Code:
function house(instanceName)
{
// move this to global scope
eval(instanceName + " = this;");
// remember instance name
this.instanceName = instanceName;
//retreive instance name
this.getInstanceName = function ()
{
return this.instanceName;
}
//default property
this.toString = function ()
{
return this.getInstanceName();
}
}
//create instances with name bobhouse and jefhouse
new house("bobhouse");
new house("jefhouse");
//should alert "bobhouse";
alert( bobhouse.getInstanceName() );
//should alert "jefhouse" (default property toString());
alert( jefhouse );
regards
Jeffrey van der Stad - D-motivatie
Last edited by D-motivatie; 08-23-2008 at 07:34 PM..
Hi there:
Is it possible to probe the name of the variable assigned to an object instance in JavaScript? For example:
Code:
function house(){
}
var bobhouse=new house()
if (instancename=="bobhouse")
//do something
In other words, can I do some sort of string comparison on the variable name "bobname", so I know it's actually called "bobname"? I feel I'm asking how to have a variable recognize itself as a variable.
After playing around, you could also do this
Code:
// JSON house: (JavaScript Object Notation)
var house = {
// method to create global instance:
create: function ( instanceName )
{
eval( instanceName + " = new house.object('" + instanceName + "')" );
},
// this is the actual created global object
object: function ( instanceName )
{
//remember instance name
this.instanceName = instanceName;
//default property
this.toString = function ()
{
return this.instanceName;
}
}
}
//create instances
house.create("bobhouse");
house.create("jefhouse");
//alert instance names
alert( bobhouse ); //alert ("bobhouse");
alert( jefhouse ); //alert ("jefhouse");
//alert instance names (using property instanceName)
alert( bobhouse.instanceName ); //alert ("bobhouse");
alert( jefhouse.instanceName ); //alert ("jefhouse");
it's more complex, but you can do so much more with complexity