Javascript 自定义类的服务器端onchange事件

Javascript 自定义类的服务器端onchange事件,javascript,class,google-apps-script,events,onchange,Javascript,Class,Google Apps Script,Events,Onchange,如果我在GoogleApps脚本中创建一个自定义类并将其分配给一个变量,那么我是否可以创建一个服务器端onchange事件,该事件将在值更改时做出反应?例如,类似于: var Polygon = function(height, width) { this.height = height; this.width = width; this.save = function() { <code to draw the polygon here ...

如果我在GoogleApps脚本中创建一个自定义类并将其分配给一个变量,那么我是否可以创建一个服务器端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=函数(高度、宽度){
高度=高度;
这个。宽度=宽度;
this.save=function(){};
}
Polygon.onchange=函数(){
currentPolygon=此值;
currentPolygon.draw();
}
var myPolygon=新多边形(10,12);
myPolygon.height=20;//触发抽签
或者,它必须包含在集合函数中吗?例如:

    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);
var Polygon=函数(高度、宽度){
var myHeight=高度;
var myWidth=宽度;
this.height=函数(){return myHeight;}
this.width=function(){return myWidth;}
this.draw=function(){};
this.changeHeight=函数(值){
myHeight=数值;
这个.draw();
}
this.changeWidth=函数(值){
myWidth=值;
这个.draw();
}
}
var myPolygon=新多边形(10,12);
myPolygon.changehight(20);

没有这样的处理程序。但您可以使用拦截所有
set
调用:

/**/console.config({maximize:true,timeStamps:false,autoScroll:false})/**/
常量多边形=函数(高度、宽度){
高度=高度;
这个。宽度=宽度;
这个值=0;
this.draw=函数(){
该值为+=1;
};
};
常量PolygonOnchangeHandler={
设定(目标、道具、价值){
Reflect.set(目标、属性、值);//调用set
Reflect.apply(target.draw,target,[]);//调用draw
},
};
const myPolygon=新代理(新多边形(10,12),PolygonOnchangeHandler);
myPolygon.height=20;//触发器绘制
console.log(myPolygon.drawing)//画一次
myPolygon.width=5;
console.log(myPolygon.drawing)//绘制两次