#Javascript intercept/customize/override/validate Object Properties with Proxy

Oscar Brito


Lately I've found the need to override the Object getter and setters to build "Dictionaries" with custom logic. For example, if you need to validate the property value before it is setted on the Object you need to use Proxy.

The following jsfiddle shows the intent ( this only works on Firefox Gecko 18 ):
    

The Proxy API is only available on ECMAScript v6 "Harmony" so if you want to use this with node you have to run node with the flag --harmonyproxies.

If you need to use this pattern but you don't have ECMAScript 6 support there is no polyfill, but you can use things like Ember.ObjectProxy or some Observable library to accomplish this.

LINKS:
http://emberjs.com/api/classes/Ember.ObjectProxy.html
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global
Objects/Proxy




Visit www.divhide.com for more informations, contacts and news about Web Development.
See other blog posts at blog.divhide.com.



Divhide purpose is to follow the HTML5 movement and contribute with applications which prove the quality of technology.


Feel free to contact divhide.