扩展 Javascript 类中的方法

发布于 2024-11-02 06:25:49 字数 652 浏览 0 评论 0原文

我(对于那些读过我以前的文章的人来说,“仍然”)正在开发 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

注定孤独终老 2024-11-09 06:25:49

像这样的东西应该有效:

var original = ToolTipPanel.showPopup;
ToolTipPanel.showPopup = function() {
   original();  //this is kind of like the call to super.showPopup()
   //your code
};

我在 Firebug 中尝试了这个简单的示例,它似乎有效:

var obj = {
   func: function() { 
     console.log("foo"); 
   }
};

obj.func();

var original = obj.func;
obj.func = function() {
   original();
   console.log("bar");
};

obj.func();

Firebug 输出:

foo
foo
bar

所以这里发生的是您保存对原始 showPopup 函数的引用。然后,您创建一个闭包并将其分配回 showPopup。原始的 showPopup 并没有丢失,因为您在 original 中仍然有对它的引用。在闭包中,您调用原始引用的函数,然后您就拥有了自己的代码。如果您想在调用original之前先执行某些操作,只需交换顺序即可。由于您使用的是闭包,因此 original 在词法上绑定到当前作用域,并且每次调用新的 showPopup 时都应该可用(如果我对此错了,有人请纠正我)。

让我知道这是否适合您。

Something like this should work:

var original = ToolTipPanel.showPopup;
ToolTipPanel.showPopup = function() {
   original();  //this is kind of like the call to super.showPopup()
   //your code
};

I tried out this trivial example in Firebug, and it seems to work:

var obj = {
   func: function() { 
     console.log("foo"); 
   }
};

obj.func();

var original = obj.func;
obj.func = function() {
   original();
   console.log("bar");
};

obj.func();

Firebug output:

foo
foo
bar

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 to showPopup. The original showPopup is not lost, because you still have a reference to it in original. In the closure, you call the function that original references, and then you have your own code. Just swap around the order if you want to do something first before you call original. Since you're using a closure, original is lexically bound to the current scope and should be available every time the new showPopup is called (if I'm wrong about this, someone please correct me).

Let me know if this works out for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文