Skip to content

Instantly share code, notes, and snippets.

@lihanli
Created December 13, 2012 01:33
Show Gist options
  • Save lihanli/4273298 to your computer and use it in GitHub Desktop.
Save lihanli/4273298 to your computer and use it in GitHub Desktop.
simple object observer
Object.defineProperty Object::, "__obs",
enumerable: false
configurable: true
writable: false
value: (prop, handler) ->
val = this[prop]
if delete this[prop] # can't watch constants
Object.defineProperty this, prop,
get: ->
val
set: (newVal) ->
val = newVal
handler.call(this, prop)
enumerable: true
configurable: true
Object.defineProperty Object::, "__unobs",
enumerable: false
configurable: true
writable: false
value: (prop) ->
val = this[prop]
delete this[prop] # remove accessors
this[prop] = val
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment