I want to define getter method on object so that when I access a property , getter method fires
object.someProperty // this fires object.get(name) where name is someProperty
this is an example illustrate my thought
file : data.js
import setting from 'setting';
const data = {
a : {
foo : 1 ,
bar : 2
} ,
b : {
foo : 3 ,
bar : 4
}
}
//DEFINE GETTER
//something like this
data.DEFINE_GETTER(function(property)
{
if(setting.type === 'a')
return data['a'][property];
else
return data['b'][property];
})
export default data;
file : test.js
import data from 'data';
import setting from 'setting';
setting.type = 'a';
console.log(data.foo) //should be 1
console.log(data.bar) //should be 2
setting.type = 'b';
console.log(data.foo) //should be 3
console.log(data.bar) //should be 4