davej Posted November 13, 2013 Share Posted November 13, 2013 If I have a function that is passed an object as a parameter -- can that function determine the name of that object? Link to comment Share on other sites More sharing options...
justsomeguy Posted November 13, 2013 Share Posted November 13, 2013 if you're trying to get the type of object, there's a discussion about that here: http://stackoverflow.com/questions/332422/how-do-i-get-the-name-of-an-objects-type-in-javascript Link to comment Share on other sites More sharing options...
davej Posted November 13, 2013 Author Share Posted November 13, 2013 It just seems odd that I can use for-in to read the keys inside an object but I can't read the name of the object. Link to comment Share on other sites More sharing options...
Ingolme Posted November 13, 2013 Share Posted November 13, 2013 The object could be anonymous. You don't need the name, just a reference to the object itself. Take this, for example: for (i in {a: 1, b: 2 } ) { document.write(i);} What's the "name" of that object? Link to comment Share on other sites More sharing options...
thescientist Posted November 13, 2013 Share Posted November 13, 2013 (edited) if it's being passed in as a parameter, wouldn't the name just be the name you give the parameter? As Ingolme pointed out, all you really need is the reference. Edited November 13, 2013 by thescientist Link to comment Share on other sites More sharing options...
davej Posted November 13, 2013 Author Share Posted November 13, 2013 (edited) I can accept the fact that anonymous items don't have names, but named items have names, and especially named globals have names. Edited November 13, 2013 by davej Link to comment Share on other sites More sharing options...
justsomeguy Posted November 13, 2013 Share Posted November 13, 2013 Objects don't have a name at all. If you look at the code linked to on the other page, it gets the name of the constructor function. Javascript isn't like other object-oriented languages, Javascript is a prototype-based language. Every object is a generic object with a constructor and whatever other properties and methods, there isn't a concept of a well-defined class with inheritance and things like that. The best you can do is get the name of the constructor. Link to comment Share on other sites More sharing options...
davej Posted November 13, 2013 Author Share Posted November 13, 2013 (edited) I guess I can make it work for globals... <!DOCTYPE html><html lang="en"><head><title>Exploding Objects</title><script src="jonerror.js"></script><script>var obj1 = {a:'aaa',b:true,c:[1,2,3],d:'ddd',e:'eee',f:function(){alert('f');},g:{aa:'aaaaaaa',bb:'bbbbbbb',cc:{x:111,y:222,z:333}}};window.onload = function(){document.getElementById('btnobj').onclick = function(){fnshow(obj1)};document.getElementById('btnclear').onclick = function(){document.getElementById('out').innerHTML='';};}function fnshow(ob){var out=document.getElementById('out');var val;var valtype;var o = '?';var globals = window;for (var p in globals) {if(globals[p]===ob){o = p;}}for (var key in ob){val = ob[key];valtype = typeof val;out.innerHTML += typeof ob +' '+ o +'.'+ key +' : '+ val +' of type '+ valtype +'<br/>';if (valtype == 'object'){fnsh(val,o+'.'+key);}}function fnsh(ob,obname){var out=document.getElementById('out');var val;var valtype;for (var key in ob){val = ob[key];valtype = typeof val;out.innerHTML += typeof ob +' '+ obname +'.'+ key +' : '+ val +' of type '+ valtype +'<br/>';if (valtype == 'object'){fnsh(val,obname+'.'+key);}}}}</script></head><body><input type="button" id="btnobj" value="Explode"/><input type="button" id="btnclear" value="Clear"/><br/><div id="out"></div></body></html> Edited November 14, 2013 by davej Link to comment Share on other sites More sharing options...
justsomeguy Posted November 13, 2013 Share Posted November 13, 2013 I'm confused, are you trying to get the name of a variable? Not the type of object (Array, String, Number, etc), but the actual variable name? Inside fnshow, you want to somehow find "obj1"? Link to comment Share on other sites More sharing options...
davej Posted November 13, 2013 Author Share Posted November 13, 2013 Yes! Link to comment Share on other sites More sharing options...
justsomeguy Posted November 14, 2013 Share Posted November 14, 2013 Oh, ok. No, there's no way to do that, in fact the two are not connected. The variable name is not associated with the object really, it's just an identifier to point to that value in memory. And likewise, any value does not have any pointer or reference to a variable that holds it. Inside the function there is no way to tell that a given parameter had a variable name before it was passed to the function. The variable is not passed to the function, the value is. The variable is just an identifier that points to the value. So inside the function, the value of the variable ob is in no way connected with the string "obj1", there's no relationship between the two. Link to comment Share on other sites More sharing options...
davej Posted November 14, 2013 Author Share Posted November 14, 2013 Well, it just seems a little crazy to need to pass both... function show(x, xname) {alert('The value of '+ xname +' is '+ x);} Link to comment Share on other sites More sharing options...
Ingolme Posted November 14, 2013 Share Posted November 14, 2013 The question is why you want to display something like that. There's no practical purpose. Objects aren't the only ones. Every single value has that problem. Take this, for example: var a = "String";var b = 5;var c = false;fnshow(a);fnshow(;fnshow(c);function fnshow(data) { var out=document.getElementById('out'); var o = '?'; out.innerHTML += typeof data +' '+ o + ' = '+ data;} Link to comment Share on other sites More sharing options...
davej Posted November 14, 2013 Author Share Posted November 14, 2013 Well, if you had a program that analyzed files you wouldn't want it to say "Beginning analysis of file ???" Link to comment Share on other sites More sharing options...
Ingolme Posted November 14, 2013 Share Posted November 14, 2013 An object, in Javascript, is a data type. It's "name", like other variable names, isn't accessible from its value. Since everything is a property of the window object, what you can do is this: var x = {a: 1};getName(x);function getName(obj) { for(i in window) { if(window[i] == obj) { alert("The object's name is " + i); } }} But really, I see absolutely no need for this in Javascript. At most you would need it for debug purposes. Link to comment Share on other sites More sharing options...
Hadien Posted November 14, 2013 Share Posted November 14, 2013 (edited) Getting the name of the Object's type is trivial. Getting a variable's identifier is a nightmare even more so inside closures. You can't reliably find the variable identifier (objects don't naturally store them) because you could have 0, 1, or even 1,000 variables referencing the same object. it would be both difficult (sometimes impossible) and unnecessary for an object to figure out which variable(s) is currently referencing it. Primitives however only have one reference variable (but since they are primitives they don't actually store any metadata about themselves just the data), if you make another variable point to a primitive, the data is copied to a new address, it doesn't reference to the exact same primitive, unlike how its done with the more advanced data structures. if you want to find the name of an Object, refer to it's constructor.name //for functions/classes/constructorsalert("Array's name is: "+Array.name);//says "Array's name is: Array"//for instancesvar arr = []alert("arr name is: "+arr.constructor.name);//says "arr name is: Array" A function's name can't be overloaded or overwritten, its a read-only attribute. anonymous functions will have an empty string and you can't change it to something meaningful after it's defined. something I didn't particularly like when I was trying to write dynamic constructors. but understandably it prevents a lot of unstable code if inexperienced programmers (or hackers) could change it on the fly. if you want the actual identifier, then use objects and key-value pairs, thats really the only way. Ingolme's post is essentially this case but on the global scale, but won't work for varibles defined in different scopes (inside functions) and closures EDIT: if you want an extremely bad way in reliably finding the variable identifier you can write a function that throws an error, catches it and use a regex on it's stack trace to find the 3rd line in the stack, loads up the file as a nonJS/html file and reads that line and parses out everything on that line except arguments in the function and returns an array of strings listing the identifiers that were passed in. then just call that function in any situation when you need to know the actual identifiers that were passed in... of course, I'm merely spouting crazy talk. Edited November 14, 2013 by Hadien 1 Link to comment Share on other sites More sharing options...
davej Posted November 14, 2013 Author Share Posted November 14, 2013 Yes, I suppose this is nonsense. Foxy posted what I found last night, so there is a work-around for globals, but I admit it would only be useful in rare cases. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now