egoist
7/31/2016 - 11:47 AM

reactive-data.js

function reactive(obj, data) {
  for (const key in data) {
    Object.defineProperty(obj, key, {
      enumerable: true,
      configurable: true,
      get() {
        if (is(data[key], 'Object')) {
          return reactive({}, data[key])
        } else if (is(data[key], 'Array')) {
          return extendArray(data[key])
        }
        return data[key]
      },
      set(val) {
        console.log(`===${key} changed===`)
        data[key] = val
      }
    })
  }
  return obj
}