JavaScript 对象链式操作测试代码 |
本文标签:JavaScript,链式操作 虽然现在慢慢减少了对jQuery的使用(项目上还是用,效率高点 。平时基本不用了),希望从而减少对jQuery的依赖度 。 但是这链式操作的方式实在吸引人(貌似现在不少新库都采用了链式操作) 。 新手无畏嘛,所以写了以下代码 。主要是避免以后又忘了,呵呵 。 复制代码 代码如下: 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()); 这只是进行了链式操作 。但是在firbug下可以看到jQuery对象返回的是数组/类数组 。要实现这个却不知道怎么办好 。 。 总不能让k.fn.prototype = new Array()吧 。真要看jQuery源代码还真是有点累 。 。 |