How to proxy JavaScript observable with getters and setters

javascript knockoutjs observables

This article is about overriding observables, so that these can be replaced with JavaScript getters and setters. 

Example in coffeescript:

constructor: (data = null) ->

# Copy `data` to separate property, so that `data`
# can be overridden
@_data = @data

# Custom getter/setter for `data`
Object.defineProperty @, 'data', {
get: () =>
return @_data

enumerable: true,
set: (value) =>
@_data = value
console.log "Have #{@_data.length} items..."