我如何懒惰评估javascript对象上的字段?

时间:2014-06-26 15:42:40

标签: javascript lazy-evaluation

我想知道是否可以执行以下操作:

var obj = {
   counter: (function(){
                if(!this.val){ this.val = 0; }
                this.val += 1;
                return this.val;
            })();
};

console.log(obj.counter); //should output 1
console.log(obj.counter); //should output 2
console.log(obj.counter); //should output 3
...

有没有办法从这样的对象中获取一个字段,以便每次访问时都重新计算一个函数?

3 个答案:

答案 0 :(得分:4)

您可以使用getter:

var obj = {};
Object.defineProperty(obj,"counter",{
    get: function() {
        this.val = this.val || 0;
        this.val++;
        return this.val;
    }
});

console.log(obj.counter); // 1
console.log(obj.counter); // 2
console.log(obj.counter); // 3

答案 1 :(得分:4)

如果您的目标平台支持proxies,则可以使用此功能:

var obj = Proxy.create({
    get: function(target, value) {
        if(value == 'counter')
            return this.val = (this.val || 0) + 1;
    }
});

console.log(obj.counter); //should output 1
console.log(obj.counter); //should output 2
console.log(obj.counter); //should output 3

另一种选择是吸气剂:

obj = Object.create({}, {
    counter: {
        get: function() {
            return this.val = (this.val || 0) + 1;
        }
    }
})

valueOf对象(这不适用于console.log,但会使用算术):

var obj = {
    counter: {
        valueOf: function() {
            return this.val = (this.val || 0) + 1;
        }
    }
};

console.log(obj.counter+5); // 6
console.log(obj.counter+5); // 7
console.log(obj.counter+5); // 8

答案 2 :(得分:0)

使用Proxy类以及用例来进行属性评估的评估需求

/* https://github.com/hack2root/lazyeval */

let lazy = (eval) => ((data) => new Proxy(data, {
  set(obj, key, val) {
  obj[key] = val;
  eval(obj);
  }
}))({});

// 1. ARRANGE
let a = 1;
let b = 2;
let c;

// 2. ACT
let func = lazy((f) => {
  if (f.a && f.b) { 
    c = f.a + f.b 
  }
});

func.a = a;
func.b = b;

// 3. ASSERT
console.log("c is", c);

let lazy_require = (requre) => (eval) =>  ((data) => new Proxy(data, {
    set(obj, key, val) {
        obj[key] = val;
        if (requre(obj)) {
            eval(obj);
        }
    }
}))({});

// 1. ARRANGE
let a_f = 1;
let b_f = 2;
let c_f;

// 2. ACT
let func_reqire = lazy_require((f) => f.a && f.b);

let lazy_func = func_reqire((f) => {
  c_f = f.a + f.b
});

lazy_func.a = a_f;
lazy_func.b = b_f;

// 3. ASSERT
console.log('c_f is', c_f);

let lazy_require_data = (requre) => (eval) => (data) => new Proxy(data, {
  set(obj, key, val) {
    obj[key] = val;
    if (requre(obj)) {
      eval(obj);
    }
  }
});

// 1. ARRANGE
let a_data = 1;
let b_data = 2;
let c_data;

// 2. ACT
let func_require_data = lazy_require_data((f) => f.a && f.b);

let func_data = func_require_data((f) => {
    c_data = f.a + f.b
});

let func_json = func_data({
    a: a_data,
    b: b_data
});

func_json.a = a;
func_json.b = b;

// 3. ASSERT
console.log('c_data is', c_data);