gpt4 book ai didi

javascript - 处理类内的事件(原型(prototype))

转载 作者:行者123 更新时间:2023-11-30 06:27:31 25 4
gpt4 key购买 nike

我正在重写问题以使其更容易回答(我希望)。我想在类(原型(prototype))中有一个按钮,但我希​​望处理程序也位于类中。如果处理程序在类外,则工作正常,但如果在类内,则无法找到它。

预先感谢您的帮助。

function myWindow(message) {  
this.SS = SpreadsheetApp.getActiveSpreadsheet();
this.App = UiApp.createApplication();
this.App.setTitle(message);
this.Panel = this.App.createVerticalPanel();
this.App.add(this.Panel);
this.Button = this.App.createButton('OK', this.App.createServerHandler('handler'));
};

myWindow.prototype = {
constructor: myWindow,
handler: function (e)
{
Browser.msgBox('Click');
},
show: function()
{
this.Panel.add(this.Button);
this.SS.show(this.App);
}
};

function Run() {
var theWindow = new myWindow('Hello World');
theWindow.show();
}

单击“确定”按钮时出现的错误是“找不到脚本函数处理程序”

最佳答案

好吧,我已经做了很多阅读,虽然没有明确说明,但 createServerHandler 似乎只是为全局函数设计的,UIAPP 中没有提供任何其他内容。这意味着我所要求的无法完成。

解决方案必须看起来像

function myWindow(message) {  
this.SS = SpreadsheetApp.getActiveSpreadsheet();
this.App = UiApp.createApplication();
this.App.setTitle(message);
this.Panel = this.App.createVerticalPanel();
this.App.add(this.Panel);
this.Button = this.App.createButton('OK', this.App.createServerHandler('handler'));
}

myWindow.prototype = {
constructor: myWindow,
show: function() {
this.Panel.add(this.Button);
this.SS.show(this.App);
}
};

function handler(e) {
Browser.msgBox('Click');
};

function Run() {
var theWindow = new myWindow('Hello World');
theWindow.show();
};

这不是 flash 但可行。

关于javascript - 处理类内的事件(原型(prototype)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20207014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com