I recommend harmony-reflect , which makes it easy, for example, to configure get / set traps:
UPDATE carefully, below - CoffeeScript
require 'harmony-reflect' handler = get: ( target, name ) -> console.log 'get' name return target[ name ] set: ( target, name, value ) -> console.log 'set' name target[ '%is-clean' ] = no if value isnt target[ name ] if value is undefined then delete target[ name ] else target[ name ] = value return value clean = ( x ) -> x[ '%is-clean' ] = yes return x p = Proxy {}, handler p[ 'a' ] = 1 p[ 'b' ] = undefined console.log p[ 'a' ], p[ 'b' ] console.log "c" of p, p[ 'c' ] console.log p clean p p[ 'a' ] = 1 console.log p p[ 'a' ] = 42 console.log p
the above code is insightful for doing "transparent save of an object" in JavaScript. using harmony-reflect , it becomes trivial to make sure that all get and set actions on the object are intercepted - in this demo we set the %is-clean attribute so that we can check if the members of the object have changed and we also delete the elements that were set to undefined.
flow Sep 19 '13 at 16:15 2013-09-19 16:15
source share