我试图创建一个可以支持方法链接的迷你jQuery克隆。到目前为止,我想出了这样一段代码:jQuery对象数据结构
var $ = (function() {
var elements = [];
function methodOne() {
console.log('Method 1');
return this;
}
function methodTwo() {
console.log('Method 2');
return this;
}
return {
methodOne: methodOne,
methodTwo: methodTwo
};
}());
在网页加载时,$
变量被由IIFE返回jQuery的克隆对象的填充。
我的问题是,我怎样才能使$
对象作为一个函数直接调用,同时仍然保持方法链接功能?
现在,我可以使用$.methodOne().methodTwo()
,但我不能像使用jQuery一样使用$('some parameter').methodOne().methodTwo()
。
我还没有尝试过,但我认为你可以使用'class' javascript和'extends' Jquery!我认为这样做。你知道,我很好奇,我也会尝试。 – funcoding