ゲッターとセッターを使用して、特定の条件を監視できます。
セッターでは、いくつかの計算を実行し、いくつかの条件が満たされているかどうかを確認し、必要に応じて更新できます。
アイデアを与えるために:
// updateFunc is the function called whenever a property changes
// and all conditions are met for an update.
// newProp1,2,3 are the new values for prop1,2,3
function MyStorageClass(updateFunc, newProp1, newProp2, newProp3 ) {
this.updateFunc = updateFunc;
this.prop1 = newProp1 ;
this.prop2 = newProp2 ;
this.prop3 = newProp3 ;
}
var MSCProto = MyStorageClass.prototype;
// update is needed if all properties are >0
MSCProto.checkUpdateRequired = function() {
return ( ( this.prop1 > 0 ) && (this.prop2 > 0) && (this.prop3 > 0) )
}
Object.defineProperty(MSCProto, 'prop1', {
get : function() { retrurn this._prop1},
set : function(x) { this._prop1 = x;
// and some other computations if need be
if (this.checkUpdateRequired()) this.updateFunc(); } };
Object.defineProperty(MSCProto, 'prop2', {
get : function() { retrurn this._prop2},
set : function(x) { this._prop2 = x;
// and some other computations if need be
if (this.checkUpdateRequired()) this.updateFunc(); } };
Object.defineProperty(MSCProto, 'prop3', {
get : function() { retrurn this._prop3},
set : function(x) { this._prop3 = x;
// and some other computations if need be
if (this.checkUpdateRequired()) this.updateFunc(); } };