扩展 Javascript 类中的方法
我(对于那些读过我以前的文章的人来说,“仍然”)正在开发 ICEFaces Web 应用程序。
这个问题可以解释为一般的 Javascript 问题,所以如果您对 ICEFaces 不太了解,请继续阅读
我需要扩展 ICEFaces Javascript 框架创建的类的行为,特别是 ToolTipPanelPopup
。
我无法修改库的源代码(否则我就达到了我的目的)。 这就是 ICEFaces 定义类的方式(很像 jQuery 和其他 Javascript 框架)。
ToolTipPanelPopup = Class.create({
[...]
showPopup: function() {
[...]
},
updateCordinate: function(event) {
[...]
},
[...]
});
我的问题非常简单,
如何扩展 showPopup() 函数的行为以便在其末尾运行我的自定义函数?
我的意思是像下面假设继承的 Java 示例代码一样
public void ShowPopup()
{
super.ShowPopup();
customMethod();
}
I'm ("still", for those who read my previous posts) working on an ICEFaces web application.
This question can be interpreted as general Javascript question, so read on if you don't know much about ICEFaces
I need to extend the behaviour of the classes created by ICEFaces Javascript framework, in particular ToolTipPanelPopup
.
I cannot modify the source code of the library (otherwise I would have achieved my goal).
This is how ICEFaces defines the class (much like jQuery and other Javascript frameworks).
ToolTipPanelPopup = Class.create({
[...]
showPopup: function() {
[...]
},
updateCordinate: function(event) {
[...]
},
[...]
});
My question is very simple
How do I extend the behaviour of showPopup()
function in order to run my custom function at the end of it?
I mean something like following Java example code that supposes inheritance
public void ShowPopup()
{
super.ShowPopup();
customMethod();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样的东西应该有效:
我在 Firebug 中尝试了这个简单的示例,它似乎有效:
Firebug 输出:
所以这里发生的是您保存对原始
showPopup
函数的引用。然后,您创建一个闭包并将其分配回showPopup
。原始的showPopup
并没有丢失,因为您在original
中仍然有对它的引用。在闭包中,您调用原始引用的函数,然后您就拥有了自己的代码。如果您想在调用original
之前先执行某些操作,只需交换顺序即可。由于您使用的是闭包,因此original
在词法上绑定到当前作用域,并且每次调用新的showPopup
时都应该可用(如果我对此错了,有人请纠正我)。让我知道这是否适合您。
Something like this should work:
I tried out this trivial example in Firebug, and it seems to work:
Firebug output:
So what's happening here is that you're saving a reference to the original
showPopup
function. Then you're creating a closure and assigning it back toshowPopup
. The originalshowPopup
is not lost, because you still have a reference to it inoriginal
. In the closure, you call the function thatoriginal
references, and then you have your own code. Just swap around the order if you want to do something first before you calloriginal
. Since you're using a closure,original
is lexically bound to the current scope and should be available every time the newshowPopup
is called (if I'm wrong about this, someone please correct me).Let me know if this works out for you.