Although I have gradually reduced my use of jQuery now (I still use it on projects, it is more efficient. I basically don’t use it in daily life), I hope to reduce my dependence on jQuery.
But this chain operation method is really attractive (it seems that many new libraries now use chain operations).
Beginners are not afraid, so I wrote the following code. The main thing is to avoid forgetting it in the future, haha.
window.k = function() {
return new k.fn.init(arguments);
}
k.fn = k.prototype = {
init:function() {
this.length = 0;
//var args = Array.prototype.slice.call(arguments,0);
Array.prototype.push.apply(this,arguments[0]);
return this;
},
show: function() {
console.log(Array.prototype.slice.call(this,0).join("$"));
return this;
},
hide:function( ) {
console.log(this);
return this;
}
}
k.fn.init.prototype = k.fn;
console.log(k( "0",1,2,3,4,5).show().hide());
This is just a chain operation. But under firbug you can see that the jQuery object returns an array/class array. I want to achieve this but don't know how to do it. .
You can’t let k.fn.prototype = new Array(). It's really tiring to look at the jQuery source code. .