动态可观察对象

时间:2015-08-01 00:51:39

标签: javascript object getter-setter object-property

我想要实现的目标:

var obj = new Dynamic({data:"something",data2:"something else"},function(val){
  console.log('"'+val+'"');
});

console.log(obj.data,obj.data2);
obj.data = "this thing here";
console.log(obj.data,obj.data2);

控制台输出

"something" "something else"
"this thing here"
"something" "something else"

我当前的控制台输出

"something else" "something else"
"this thing here"
"this thing here" "this thing here"

我的Dynamic方法看起来像这样:

function Dynamic(obj,method){
  var ret;
  var Update = method;
  if(obj instanceof Object){
    ret = {};
    for(var a in obj){
      Object.defineProperty(ret,a,{
        set: function(v){
          inner = v;
          Update.call(this,inner);
        },
        get: function(){
          return inner;
        }
      });
      ret[a] = (obj[a] instanceof Array||obj[a] instanceof Object)?Dynamic(obj[a],Update):obj[a];
    }
  }
  return ret;
}

这个问题是,每个属性都引用inner,而不是它们自己的内部版本

我试过了

Object.defineProperty(ret,a,{
//set: {dadedadeda}
//get: {dadedadeda}
value:obj[a]});

但这不起作用,因为this issue with get, set, and value这在我看来是一个巨大的疏忽(如果我们想要一个值但又想拦截这些电话怎么办?)

我也试过

Object.defineProperty(ret,a,{
//set: {dadedadeda}
//get: {return this.inner;}
inner:obj[a]});

但似乎这是不会发生的事情,this无论如何都指的是整个对象,

1 个答案:

答案 0 :(得分:0)

当我正在考虑这个问题时,我实际上找到了答案:JavaScript Closures(谁会想到)

我的完整代码看起来像这样

function Observable(obj, method) {
  function DefineDescriptor(object, key, method, value) {
    var inner = value; // this is the private value of our property
    var descriptor = {
      set: function(v) {
        inner = (v instanceof Object) ? Observable(v, method) : v; // every object added from this point will invoke the method defined
        method.call(this, key, v);
      },
      get: function() {
        return inner;
      }
    };
    Object.defineProperty(object, key, descriptor);
  }
  var ret;
  if (obj instanceof Object) {
    ret = {};
    for (var a in obj) {
      DefineDescriptor(ret, a, method, (obj[a] instanceof Object) ? Observable(obj[a], method) : obj[a]);
    }
  }
  return ret;
}
document.addEventListener("DOMContentLoaded", function(loadEvt) {
  var B = Observable({
    key: "value",
    nested: {
      key: 123,
      str: "hey"
    }
  }, function(key, val) {
    console.log(this, key, this[key], val);
  });
});