Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating generic getters and setter on javascript object

Tags:

javascript

It is possible to create getters and setters in javascript as shown by

Object.defineProperty
__define***__

In all those instances, the name of the property is known.

Is it possible create a generic one.

By this I mean, I have a getter and or setter and it is called irrespective of the property name.

Is this possible? If so, how?

regards.

Note: I did find these after posting the question. Looks like it is currently not possible as the first answer stated.

Is it possible to implement dynamic getters/setters in JavaScript?

Monitor All JavaScript Object Properties (magic getters and setters)

like image 412
ritcoder Avatar asked Jan 26 '12 23:01

ritcoder


1 Answers

To all time travelers like me:

It was not possible at the time the question was asked, but it is already possible in present versions of EcmaScript via so-called proxy objects. See more here:

  • Is it possible to implement dynamic getters/setters in JavaScript?
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
like image 116
jaboja Avatar answered Sep 27 '22 17:09

jaboja