jQuery: extend plugin question

时光怂恿深爱的人放手 提交于 2019-12-23 01:54:07

问题


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


回答1:


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);


来源:https://stackoverflow.com/questions/1885364/jquery-extend-plugin-question

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!