Javascript ClassyDojo-内部方法调用
我完全是dojo的初学者,我正试图将我的一些接口代码移动到一个类中,只是为了不让方法出现在我的主文件中 我的问题-我无法将内部类函数用作其他函数的一部分 如图所示,在外部文件中创建接口对象后,我能够成功地:Javascript ClassyDojo-内部方法调用,javascript,dojo,Javascript,Dojo,我完全是dojo的初学者,我正试图将我的一些接口代码移动到一个类中,只是为了不让方法出现在我的主文件中 我的问题-我无法将内部类函数用作其他函数的一部分 如图所示,在外部文件中创建接口对象后,我能够成功地: appInterface = new (interface) appInterface.showGraphWindow() appInterface.hideGraphWindow() 但是,我无法确定如何在toggleGraphWindow函数中使用这些函数。(由于上下文?) 如果我试图
appInterface = new (interface)
appInterface.showGraphWindow()
appInterface.hideGraphWindow()
但是,我无法确定如何在toggleGraphWindow函数中使用这些函数。(由于上下文?)
如果我试图打电话:
on(registry.byId("graphBtn"),"click", appInterface.toggleGraphWindow);
它在线路上崩溃:
this.showGraphWindow()
或
带:“未定义是一个非函数”
如何对toggleGraphWindow函数进行编码
Iterface.js
你怎么了
toggleGraphWindow : function (){
if (dijit.byId("graphBtn").checked == true) {
this.showGraphWindow();
}
else {
this.hideGraphWindow();
}
}
谢谢你们两位,你的确是对的,肯,我已经读了很多次类似的帖子,但不知怎么的,我不明白其中的答案: 在阅读了你的帖子后,我不知怎么地理解了上面链接的答案,现在明白了我的问题是什么了!谢谢大家 我通过修改主应用程序中的代码修复了它,如下所示:
var appInterface = new Interface();
on(registry.byId("graphBtn"),"click", appInterface.toggleGraphWindow);
改为:
var appInterface = new Interface();
var graphToggle = dojo.hitch(appInterface, "toggleGraphWindow");
on(registry.byId("graphBtn"),"click", graphToggle);
我认为错误的原因是,运行时的“this”对象实际上是“graphBtn”,而不是appInterface。我猜这里真正的问题可能与调用toggleGraphWindow有关?您没有显示任何调用它的代码<代码>挂接在这里使用它的方式在两种意义上完全没有任何作用-1,
挂接
返回一个函数,因此您正在生成一个函数,并且从不调用它;2,如果您在某个地方遇到上下文问题,在这里调用hitch
已经太迟了,无法解决它,因为这个
已经错了-这又回到了我原来的问题。我试图在主应用程序中调用它:var-appInterface=new Interface();appInterface.hideGraphWindow();appInterface.toggleGraphWindow();我最初的代码是Jan所示的,但是我在'this.showGraphWindow行(在切换函数中)上的代码错误是“undefined is not a function”
var appInterface = new Interface();
on(registry.byId("graphBtn"),"click", appInterface.toggleGraphWindow);
var appInterface = new Interface();
var graphToggle = dojo.hitch(appInterface, "toggleGraphWindow");
on(registry.byId("graphBtn"),"click", graphToggle);