This is my use case
getSomeFields(persons, fields){
let personsWithSpecificFields = [];
_.each(persons, (person) => {
let personSpecificFields = {};
_.each(fields, (field) => {
// here im thinking to modify the field to match the method name
// ( if something like __call as in php is available)
// e.g. field is first_name and i want to change it to getFirstName
personSpecificFields[field] = person[field]();
});
personsWithSpecificFields.push(personSpecificFields);
});
return personsWithSpecificFields;
}
Here is my person class
import _ from 'lodash';
export default class Person{
// not working..
__noSuchMethod__(funcName, args){
alert(funcName);
}
constructor( data ){
this.init(data);
}
init(data) {
_.each(data, (value, key) => {
this[key] = value;
});
}
}
I have gone through Monitor All JavaScript Object Properties (magic getters and setters), tried to implement this JavaScript getter for all properties but no luck.
I know i can do this by just writing another method which will transform my first_name
to getFirstName
and give it a shot. But is there any way to do this in ECMA-6
way like in class.
thanks.