How to get the name of the parent object of the Javascript property

1

I have the following code:

 var teste = 'objeto': {
           version: '5.x.x',
           name:this[??]  // 'objeto'
        };

What could be done so that the name property has the same name as the object to which it belongs (which in this case is objeto )?

    
asked by anonymous 09.03.2015 / 20:54

1 answer

3

There is no special syntax to do what you want. Objects in javascript have no "parent" pointer (after all, if there was a difference in whether an object is stored in a table, list, or variable - would be a major headache ...)

If you want to avoid typing the string "object" twice, something you can do is by that name in a variable and assign fields dynamically:

var myName = 'objeto';
var teste = {};
teste[myName] = {version: '5.x.x', name:myName}
    
09.03.2015 / 21:07