覆盖 YUI Javascript 对象

发布于 2024-12-10 06:49:38 字数 773 浏览 0 评论 0原文

我正在努力尝试修改一些 JavaScript 代码。我知道我想要更改什么,但它位于定期更新产品的巨大 JS 文件中。它基本上是对产品的小型定制。我认为可以将我的更改外部化并保持核心文件完整,而不是直接破解文件。

我想要更改的 JS 文件的结构如下所示:

(function()
{
   Company.CoolWidget = function(inArg)
   {
      Company.CoolWidget.superclass.constructor.call(this, inArg);

   };

   YAHOO.lang.augmentObject(Company.CoolWidget.prototype,
   {
      options:
      {
          ....
      },
      onReady: function COOL_onReady()
      {
          ....
      }
   }, true);
})();

我想要在标准 onReady (COOL_onReady()) 函数中执行所有操作,但在其末尾添加一些内容。 我想添加一个新方法,例如:

onNewMethod: function EXT_COOL_newMethod()

我不确定如何做到这一点以及我是否可以做到这一点,并且我不确定在其他 JS 文件之后加载新的 JS 文件是否一定能保证将会发生覆盖。

或者也许问题应该是:如何以最小的侵入性对 YUI2 Javascript 文件进行更改。

I'm struggling with trying to modify some Javascript code. I know what I want to change, but it is within a huge JS file of a regularly updated product. It's basically a small customization to a product. Rather than hack the file directly, I thought it might be possible to externalize my changes and leave the core file intact.

The structure of the JS file that I'd like to change looks like:

(function()
{
   Company.CoolWidget = function(inArg)
   {
      Company.CoolWidget.superclass.constructor.call(this, inArg);

   };

   YAHOO.lang.augmentObject(Company.CoolWidget.prototype,
   {
      options:
      {
          ....
      },
      onReady: function COOL_onReady()
      {
          ....
      }
   }, true);
})();

I want to do everything in the standard onReady (COOL_onReady()) function, but add something to the end of it.
And I want to add a new method, something like:

onNewMethod: function EXT_COOL_newMethod()

I'm not sure how to do that and whether I even can do that, and I'm not sure if just loading the new JS file after the other JS file necessarily guarantees that the overwrite will happen.

Or maybe the question should be: How can I be minimally invasive in making changes to a YUI2 Javascript file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

把昨日还给我 2024-12-17 06:49:38

我遇到了与此类似的问题,但我试图完成的是错误修复。我想你也可以认为这是一样的。

Y.Button.prototype._setTitle = function (p_sTitle) { 
  // http://yuilibrary.com/forum/viewtopic.php?p=885
  /*jslint nomen: true*/
  var sTitle = p_sTitle;
  if (!sTitle) {
    switch (this.get("type")) {
    case "radio":
      sTitle = this.RADIO_DEFAULT_TITLE;
      break;
    case "checkbox":
      sTitle = this.CHECKBOX_DEFAULT_TITLE;
      break;
    case "menu":
      sTitle = this.MENUBUTTON_DEFAULT_TITLE;
      break;
    case "split":
      sTitle = this.SPLITBUTTON_DEFAULT_TITLE;
      break;
    case "submit":
      sTitle = this.SUBMIT_TITLE;
      break;
    }
  }
  /*jslint nomen: false*/
  this._button.title = sTitle;
  /*jslint nomen: true*/
};

我相信原型就是您所追求的。上面的 yulibrary 链接可能会有所帮助。

祝你好运! :)

I had a problem similar to this, but what I was trying to accomplish was a bug fix. I suppose you could consider this the same.

Y.Button.prototype._setTitle = function (p_sTitle) { 
  // http://yuilibrary.com/forum/viewtopic.php?p=885
  /*jslint nomen: true*/
  var sTitle = p_sTitle;
  if (!sTitle) {
    switch (this.get("type")) {
    case "radio":
      sTitle = this.RADIO_DEFAULT_TITLE;
      break;
    case "checkbox":
      sTitle = this.CHECKBOX_DEFAULT_TITLE;
      break;
    case "menu":
      sTitle = this.MENUBUTTON_DEFAULT_TITLE;
      break;
    case "split":
      sTitle = this.SPLITBUTTON_DEFAULT_TITLE;
      break;
    case "submit":
      sTitle = this.SUBMIT_TITLE;
      break;
    }
  }
  /*jslint nomen: false*/
  this._button.title = sTitle;
  /*jslint nomen: true*/
};

Prototype, I believe, is what you are after. The yuilibrary link above might be of assistance.

Good luck! :)

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