Get object property name as a string

后端 未结 13 581
难免孤独
难免孤独 2020-12-04 18:40

Is it possible to get the object property name as a string

person = {};
person.first_name = \'Jack\';
person.last_name = \'Trades\';
person.address = {};
per         


        
13条回答
  •  臣服心动
    2020-12-04 19:20

    No, it's not possible.

    Imagine this:

    person.age = 42;
    person.favoriteNumber = 42;
    
    var pn = propName(person.age)
    // == propName(42)
    // == propName(person.favoriteNumber);
    

    The reference to the property name is simply lost in that process.

提交回复
热议问题