Home  >  Article  >  Web Front-end  >  JavaScript object chain operation test code_js object-oriented

JavaScript object chain operation test code_js object-oriented

WBOY
WBOYOriginal
2016-05-16 18:28:501121browse

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.

Copy code The code is as follows:

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. .
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn