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);
}
}
令人敬畏的工作@Bergi。我不知道。这非常令人高兴,我假设非常容易瞄准我自己的脚。 :) – 2014-09-04 18:28:04
是的,调用这个数据属性(而不是一个访问器)会伤害例如:-) – Bergi 2014-09-04 18:33:18
我在IE10上运行此代码时未将可配置设置为true并且运行良好。当我在可配置属性上查找时,我发现它默认为false。奇怪的。 – 2014-09-04 19:24:46