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

Asked

Viewed 101 times

1

I have the following code:

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

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

  • The syntax you have is wrong, you can’t name objects that way. You can explain better what you want to do?

  • Are you sure it’s not {'objeto':{...}} instead of 'objeto':{...}?

  • That’s right, I didn’t care much for the syntax because the focus is the concept, but actually properties in string format are only accepted when inside an object

1 answer

3


There is no special syntax to do what you want. Javascript objects have no pointer pro "father" (after all, if there was a difference if an object is stored in a table, list or variable - it would be a big headache...)

If what you want is to avoid writing 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}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.