扩展$ .fn.init函数
我正在尝试使用以下内容扩展jQuery:
$.fn.extend({ initCore: $.fn.init, init: function (selector, context, rootjQuery) { return $.fn.initCore(selector, context, rootjQuery); } }),
但是,它似乎不正常,并且创建简单的事情(例如点击警报)会产生错误。 谁能发现问题?
提前致谢!
$.fn.init
是类构造函数本身。
尝试添加$.fn.init.prototype = $.fn
来恢复原始原型。
我想你错过了上下文。 尝试
return $.fn.initCore.call(this, selector, context, rootjQuery);
甚至更容易
return this.initCore(selector, context, rootjQuery);
不,等等, init
是构造函数本身。 这意味着
$.fn.initCore.call(this, selector, context, rootjQuery); doSomeThingWith(this); ... $.fn.init.prototype = $.fn;
要么
var ob = new $.fn.initCore(selector, context, rootjQuery); doSomeThingWith(ob); return ob;