Get object property name as a string

后端 未结 13 580
难免孤独
难免孤独 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:32

    Following up on @David Sherret's answer with ES6 it can be made super simple:

    propName = f => /\.([^\.;]+);?\s*\}$/.exec(f.toString())[1]
    let prop = propName(() => {obj.name}); // myProperty
    

提交回复
热议问题