2014-09-04 34 views
0

有没有办法在defineProperty中捕获Javascript集函数,执行一些逻辑,然后决定是否真的允许调用原始的set函数?有条件地调用/阻止Javascript的defineProperty对象中的setter的调用

var scope = {}; 
scope.myVar = 1; 

scope._myVar = scope.myVar; 
Object.defineProperty(scope, "myVar", { 
    get: function(){ 
    return scope._myVar; 
    }, 
    set: function(val) { 
    scope._myVar = val; 
    //Do some other work 
    } 
} 

//Now when scope.myVar is changed, its setter is invoked 
//I would like to write some code here now that will run even before the 
//myVar setter, do some work, and then decide whether to invoke the setter 
//or not. If it decides to not invoke the setter, then it will be as 
//though the scope.myVar = ... was never called. 

//Psuedo-code 
scope._setMyVar = scope.setMyVar; 
scope.setMyVar = function(val) { 
    //do some work 
    var condition = resultOfWorkAbove; 

    if(condition) { 
    scope._setMyVar(val); 
    } 
} 

回答

2

是的,有。你可以用Object.getOwnPropertyDescriptor()得到旧的setter(你在伪代码中写成scope._setMyVar = scope.setMyVar;)。

(function(obj, prop) { // an IEFE for local variables 
    var desc = Object.getOwnPropertyDescriptor(obj, prop), 
     oldsetter = desc.set; 
    desc.set = function(val) { 
     var condition = … // do some work; 
     if (condition) 
      oldsetter.call(this, val); 
    }; 
    Object.defineProperty(obj, prop, desc); 
}(scope, "myVar")); 

当然,如果原始属性描述了configurable set to true这样做才有效,否则就无法覆盖它。

+0

令人敬畏的工作@Bergi。我不知道。这非常令人高兴,我假设非常容易瞄准我自己的脚。 :) – 2014-09-04 18:28:04

+1

是的,调用这个数据属性(而不是一个访问器)会伤害例如:-) – Bergi 2014-09-04 18:33:18

+0

我在IE10上运行此代码时未将可配置设置为true并且运行良好。当我在可配置属性上查找时,我发现它默认为false。奇怪的。 – 2014-09-04 19:24:46