2

i want to use dynamic set and get method in es6 class

EX:

class Employee {
   constructor (props) {
     this.data = props
   }

   set (key, vals) {
      this.data[key] = vals
   }

   get (key) {
      return this.data[key]
   }
}


var e = new Employee({name: 'test'})

e.name // test

e.address = "USA" // this set address field in data object

i want to add dynamic getter and setter for get and set dynamic class property.

i want generic set and get function not only for single property like EX:

set name(val)
get name()
Deep Patel
  • 301
  • 2
  • 3
  • 10
  • 5
    You might be looking for a `Proxy`, but what is your [actual problem](https://meta.stackexchange.com/q/66377)? What do you need getters and setter for? – Bergi Mar 12 '18 at 18:49
  • see the answer here: https://stackoverflow.com/questions/10592753/how-to-define-setter-getter-on-prototype – David Ibl Mar 12 '18 at 18:50
  • 2
    what arbitrary properties might an `Employee` have, that you'd need these generic getter/setter? – Thomas Mar 12 '18 at 19:14

0 Answers0