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 14 of 14
  1. #1
    WA
    WA is offline
    Administrator
    Join Date
    Mar 2002
    Posts
    2,596
    Thanks
    2
    Thanked 19 Times in 18 Posts

    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.

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,776
    Thanks
    19
    Thanked 155 Times in 146 Posts

    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 and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #3
    WA
    WA is offline
    Administrator
    Join Date
    Mar 2002
    Posts
    2,596
    Thanks
    2
    Thanked 19 Times in 18 Posts
    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.

  • #4
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,776
    Thanks
    19
    Thanked 155 Times in 146 Posts
    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 and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,299
    Thanks
    10
    Thanked 585 Times in 566 Posts
    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) )
    Last edited by rnd me; 04-08-2008 at 11:34 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #6
    WA
    WA is offline
    Administrator
    Join Date
    Mar 2002
    Posts
    2,596
    Thanks
    2
    Thanked 19 Times in 18 Posts
    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.

  • #7
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,776
    Thanks
    19
    Thanked 155 Times in 146 Posts
    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 and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #8
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,299
    Thanks
    10
    Thanked 585 Times in 566 Posts
    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.
    Last edited by rnd me; 04-09-2008 at 01:45 AM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #9
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,776
    Thanks
    19
    Thanked 155 Times in 146 Posts
    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 and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #10
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    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

  • #11
    WA
    WA is offline
    Administrator
    Join Date
    Mar 2002
    Posts
    2,596
    Thanks
    2
    Thanked 19 Times in 18 Posts
    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.

  • #12
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #13
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #14
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    1
    Thanks
    0
    Thanked 1 Time in 1 Post
    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

  • Users who have thanked md9547 for this post:

    Philip M (11-25-2010)


  •  

    Posting Permissions

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