自定义 class 的服务器端 onchange 事件
Server side onchange event for custom class
如果我在 Google Apps 脚本中创建一个自定义 class 并将其分配给一个变量,我可以创建一个服务器端 onchange 事件,该事件会在值更改时做出反应吗?例如,类似于:
var Polygon = function(height, width) {
this.height = height;
this.width = width;
this.save = function() { <code to draw the polygon here ...> };
}
Polygon.onchange = function() {
currentPolygon = this.value;
currentPolygon.draw();
}
var myPolygon = new Polygon(10, 12);
myPolygon.height = 20; // triggers draw
或者,它必须包含在集合函数中吗?例如:
var Polygon = function(height, width) {
var myHeight = height;
var myWidth = width;
this.height = function() { return myHeight; }
this.width = function() { return myWidth; }
this.draw = function() { <code to draw the polygon here ...> };
this.changeHeight = function(value) {
myHeight = value;
this.draw();
}
this.changeWidth = function(value) {
myWidth = value;
this.draw();
}
}
var myPolygon = new Polygon(10, 12);
myPolygon.changeHeight(20);
没有这样的处理程序。但是你可以使用 proxy 拦截所有 set
调用:
/*<ignore>*/console.config({maximize:true,timeStamps:false,autoScroll:false});/*</ignore>*/
const Polygon = function(height, width) {
this.height = height;
this.width = width;
this.drawn = 0;
this.draw = function() {
this.drawn += 1;
};
};
const PolygonOnchangeHandler = {
set(target, prop, value) {
Reflect.set(target, prop, value);//calls set
Reflect.apply(target.draw, target, []);//calls draw
},
};
const myPolygon = new Proxy(new Polygon(10, 12), PolygonOnchangeHandler);
myPolygon.height = 20; // trigges draw
console.log(myPolygon.drawn);//drawn once
myPolygon.width = 5;
console.log(myPolygon.drawn);//drawn twice
<!-- https://meta.whosebug.com/a/375985/ --> <script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
如果我在 Google Apps 脚本中创建一个自定义 class 并将其分配给一个变量,我可以创建一个服务器端 onchange 事件,该事件会在值更改时做出反应吗?例如,类似于:
var Polygon = function(height, width) {
this.height = height;
this.width = width;
this.save = function() { <code to draw the polygon here ...> };
}
Polygon.onchange = function() {
currentPolygon = this.value;
currentPolygon.draw();
}
var myPolygon = new Polygon(10, 12);
myPolygon.height = 20; // triggers draw
或者,它必须包含在集合函数中吗?例如:
var Polygon = function(height, width) {
var myHeight = height;
var myWidth = width;
this.height = function() { return myHeight; }
this.width = function() { return myWidth; }
this.draw = function() { <code to draw the polygon here ...> };
this.changeHeight = function(value) {
myHeight = value;
this.draw();
}
this.changeWidth = function(value) {
myWidth = value;
this.draw();
}
}
var myPolygon = new Polygon(10, 12);
myPolygon.changeHeight(20);
没有这样的处理程序。但是你可以使用 proxy 拦截所有 set
调用:
/*<ignore>*/console.config({maximize:true,timeStamps:false,autoScroll:false});/*</ignore>*/
const Polygon = function(height, width) {
this.height = height;
this.width = width;
this.drawn = 0;
this.draw = function() {
this.drawn += 1;
};
};
const PolygonOnchangeHandler = {
set(target, prop, value) {
Reflect.set(target, prop, value);//calls set
Reflect.apply(target.draw, target, []);//calls draw
},
};
const myPolygon = new Proxy(new Polygon(10, 12), PolygonOnchangeHandler);
myPolygon.height = 20; // trigges draw
console.log(myPolygon.drawn);//drawn once
myPolygon.width = 5;
console.log(myPolygon.drawn);//drawn twice
<!-- https://meta.whosebug.com/a/375985/ --> <script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script>