How to get object name in javascript?

For example, I have an object like this:

var a = { 'light': 'good', 'dark' : { 'black': 'bad', 'gray' : 'not so bad' } } 

and such a code:

 var test = function(obj) { // do smth with object // I need to get obj name ('dark' in my way) } test(a.dark); 

How to get the name of an object in the function body. Therefore, I want to say that I should know that the name obj is "dark".

I tried to test the object with firebug, but it only shows the property of the object. It does not show some internal methods or properties with which I will know.

Thanks.

+6
javascript object
source share
5 answers

You can not. You pass the object { black : 'bad', gray : 'not so bad' } only to test . This object does not have the name “dark”, it is just an object that existed as a dark property of object a . This information is irretrievably lost when passing it to a function.

Basically you are trying to get a variable name containing a value before the passed value is passed to the function. It's impossible.

+11
source share

An “object name” is not an internal property of an object. "Name" is the name in this context. When you pass an object to a function, you simply pass that object, not the context in which it was named ("dark" in your example).

Whatever you choose, you are mistaken.

+3
source share

I would like to point out the possibility of iterating over an object and find the name of the parent of a property recursively. With it, the test function will look like this:

 var test = function(rootobj,propname,rootObjName) { // do smth with object AS rootobj[propname] var objparents = findParents(rootobj,propname,rootObjName); } test(a,'dark','a'); 

Where findParents :

 function findParents(obj,key,rootName){ var parentName = rootname || 'ROOT', result = []; function iterate(obj, doIndent){ var parentPrevName = '' for (var property in obj) { if (obj.hasOwnProperty(property)){ if (obj[property].constructor === Object) { parentPrevName = parentName; parentName = property; iterate(obj[property]); parentName = parentPrevName; } if (key === property) { result.push('Found parent for key [' +key+' (value: '+obj[property] +')] => '+parentName); } } } } iterate(obj); return result; } 

The problem, of course, is that the property does not have to be unique. How in:

 var a = { 'light': 'good', 'dark' : { 'black': 'bad', 'gray' : 'not so bad' 'yellow' : { 'dark': 'will do', //<=there 'dark' again! 'light':'never use' } } } 

Well, maybe it can be used. You can find a demo of the findParents function at http://jsfiddle.net/KooiInc/Kj2b9/

+3
source share

It is possible with

 function loadProps(obj, container) { for (var p in obj) { container.push(p); if (obj[p].constructor == Object) { loadProps(obj[p], container); } } } 

then:

 var props = []; loadProps(a, props); console.log( props.join(",") ); 
+3
source share

var a = { name:'a', 'light': 'good', 'dark' : { name: 'dark', 'black': 'bad', 'gray' : 'not so bad' } }

So you can do

 console.log(a.name,a.dark.name); 
+1
source share

All Articles