jQuery:扩展插件问题

发布于 2024-08-14 12:12:52 字数 534 浏览 8 评论 0原文

我有这个简单的插件代码:

(function ($) {
  $.fn.tWeb = function () 
  {
    var me = this;
    me.var1 = "foo";

    this.done = function()
    {
        return this;
    }
    return this.done();
  };

})(jQuery);

var web = new jQuery.fn.tWeb();
alert(web.var1);

工作得很好 -alert(web.var1) 给了我“foo”。

我的问题:是否可以通过简单地包含另一个具有更多代码的 .js 来扩展此插件?例如。我可以要求 web.var2

我以前使用过一个原型函数,并且可以通过简单地添加另一个引用它的 js-include 来“扩展”它,例如。就像 tWeb.prototype.newfunction = function() 一样,

这如何用 jQuery 来完成?

谢谢

i'm having this simple plugin code:

(function ($) {
  $.fn.tWeb = function () 
  {
    var me = this;
    me.var1 = "foo";

    this.done = function()
    {
        return this;
    }
    return this.done();
  };

})(jQuery);

var web = new jQuery.fn.tWeb();
alert(web.var1);

works nice - alert(web.var1) is giving me "foo".

my question: would it be possible extending this plugin by simply including another .js which has more code? eg. that i could ask for web.var2

i previously used a prototype function and could "extend" it by simply adding another js-include which refered to it eg. like tWeb.prototype.newfunction = function()

how could this be done with jQuery?

thx

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

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

发布评论

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

评论(1

叶落知秋 2024-08-21 12:12:52

这是可能的,但我不确定这是一个很好的做法。

但是,您可以这样做:

jQuery.fn.tWeb.prototype.newfunction = function();

从风格 POV 出发,增强插件闭包中的原型可能会更好。

(function ($) {
  $.fn.tWeb = function () 
  {
    var me = this;
    me.var1 = "foo";

    this.done = function()
    {
        return this;
    }
    return this.done();
  };

  $.fn.tWeb.prototype.newFunction = function() {};



})(jQuery);

It is possible, but I'm not sure it's a great practice.

However, you could do something this:

jQuery.fn.tWeb.prototype.newfunction = function();

It might be better, from a stylistic POV, to enhance the prototype within the plugin closure.

(function ($) {
  $.fn.tWeb = function () 
  {
    var me = this;
    me.var1 = "foo";

    this.done = function()
    {
        return this;
    }
    return this.done();
  };

  $.fn.tWeb.prototype.newFunction = function() {};



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