Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-08-2008, 08:25 PM   PM User | #1
WA
Administrator


 
Join Date: Mar 2002
Posts: 2,596
Thanks: 2
Thanked 19 Times in 18 Posts
WA will become famous soon enough
Get name of object instance's variable?

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.
WA is offline   Reply With Quote
Old 04-08-2008, 08:57 PM   PM User | #2
chump2877
Senior Coder

 
chump2877's Avatar
 
Join Date: Dec 2004
Location: the U.S. of freakin' A.
Posts: 2,549
Thanks: 15
Thanked 131 Times in 124 Posts
chump2877 is on a distinguished road
Macintosh

outside of making the variable name a property of the object, I can;t think of a better way:

Code:
function house(id)
{
    this.id = id;
}

var bobhouse = new house("bobhouse")

if (bobhouse.id == "bobhouse")
{
    alert("it works");
}
__________________
Regards, R.J.

---------------------------------------------------------

Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
[Related videos and tutorials are also available at my YouTube channel]
chump2877 is offline   Reply With Quote
Old 04-08-2008, 09:17 PM   PM User | #3
WA
Administrator


 
Join Date: Mar 2002
Posts: 2,596
Thanks: 2
Thanked 19 Times in 18 Posts
WA will become famous soon enough
Ah thanks, but I was looking for a way to dynamically detect the variable instance's name, instead of manually remembering it.
__________________
- George
- JavaScript Kit- JavaScript tutorials and 400+ scripts!
- JavaScript Reference- JavaScript reference you can relate to.
WA is offline   Reply With Quote
Old 04-08-2008, 09:41 PM   PM User | #4
chump2877
Senior Coder

 
chump2877's Avatar
 
Join Date: Dec 2004
Location: the U.S. of freakin' A.
Posts: 2,549
Thanks: 15
Thanked 131 Times in 124 Posts
chump2877 is on a distinguished road
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.
__________________
Regards, R.J.

---------------------------------------------------------

Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
[Related videos and tutorials are also available at my YouTube channel]
chump2877 is offline   Reply With Quote
Old 04-08-2008, 11:31 PM   PM User | #5
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,553
Thanks: 9
Thanked 480 Times in 463 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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%

Last edited by rnd me; 04-08-2008 at 11:34 PM..
rnd me is offline   Reply With Quote
Old 04-09-2008, 12:36 AM   PM User | #6
WA
Administrator


 
Join Date: Mar 2002
Posts: 2,596
Thanks: 2
Thanked 19 Times in 18 Posts
WA will become famous soon enough
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.
WA is offline   Reply With Quote
Old 04-09-2008, 12:45 AM   PM User | #7
chump2877
Senior Coder

 
chump2877's Avatar
 
Join Date: Dec 2004
Location: the U.S. of freakin' A.
Posts: 2,549
Thanks: 15
Thanked 131 Times in 124 Posts
chump2877 is on a distinguished road
Quote:
Originally Posted by rnd me View Post
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?
__________________
Regards, R.J.

---------------------------------------------------------

Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
[Related videos and tutorials are also available at my YouTube channel]
chump2877 is offline   Reply With Quote
Old 04-09-2008, 01:36 AM   PM User | #8
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,553
Thanks: 9
Thanked 480 Times in 463 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by chump2877 View Post
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%

Last edited by rnd me; 04-09-2008 at 01:45 AM..
rnd me is offline   Reply With Quote
Old 04-09-2008, 03:36 AM   PM User | #9
chump2877
Senior Coder

 
chump2877's Avatar
 
Join Date: Dec 2004
Location: the U.S. of freakin' A.
Posts: 2,549
Thanks: 15
Thanked 131 Times in 124 Posts
chump2877 is on a distinguished road
Quote:
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...
__________________
Regards, R.J.

---------------------------------------------------------

Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
[Related videos and tutorials are also available at my YouTube channel]
chump2877 is offline   Reply With Quote
Old 04-09-2008, 04:57 AM   PM User | #10
Trinithis
Regular Coder

 
Join Date: Jun 2007
Location: USA
Posts: 527
Thanks: 26
Thanked 74 Times in 72 Posts
Trinithis will become famous soon enough
Quote:
Originally Posted by WA View Post
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.
Trinithis is offline   Reply With Quote
Old 04-09-2008, 08:34 AM   PM User | #11
WA
Administrator


 
Join Date: Mar 2002
Posts: 2,596
Thanks: 2
Thanked 19 Times in 18 Posts
WA will become famous soon enough
Quote:
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.
WA is offline   Reply With Quote
Old 08-23-2008, 07:28 PM   PM User | #12
D-motivatie
New to the CF scene

 
Join Date: Aug 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
D-motivatie is an unknown quantity at this point
Quote:
Originally Posted by WA View Post
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..
D-motivatie is offline   Reply With Quote
Old 08-23-2008, 07:47 PM   PM User | #13
D-motivatie
New to the CF scene

 
Join Date: Aug 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
D-motivatie is an unknown quantity at this point
Quote:
Originally Posted by WA View Post
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

Regards

Jeffrey van der Stad - D-motivatie
D-motivatie is offline   Reply With Quote
Old 11-24-2010, 09:22 PM   PM User | #14
md9547
New to the CF scene

 
Join Date: Nov 2010
Posts: 1
Thanks: 0
Thanked 1 Time in 1 Post
md9547 is an unknown quantity at this point
I know this is an old thread but as it's top on google for the question I'll post the solution I just found to this: http://www.liam-galvin.co.uk/2010/11...n-object/#read
md9547 is offline   Reply With Quote
Users who have thanked md9547 for this post:
Philip M (11-25-2010)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:41 PM.


Advertisement
Log in to turn off these ads.