JavaScript 对象链式操作测试代码
2010-04-25 00:00
726 查看
虽然现在慢慢减少了对jQuery的使用(项目上还是用,效率高点。平时基本不用了),希望从而减少对jQuery的依赖度。
但是这链式操作的方式实在吸引人(貌似现在不少新库都采用了链式操作)。
新手无畏嘛,所以写了以下代码。主要是避免以后又忘了,呵呵。
这只是进行了链式操作。但是在firbug下可以看到jQuery对象返回的是数组/类数组。要实现这个却不知道怎么办好。。
总不能让k.fn.prototype = new Array()吧。真要看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源代码还真是有点累。。
相关文章推荐
- JavaScript 对象链式操作测试代码
- JavaScript对象链式操作代码(jquery)
- JavaScript对象链式操作代码(jquery)
- JavaScript基础语法测试代码,覆盖JavaScript五大对象和function!
- JavaScript基础语法测试代码,覆盖JavaScript五大对象和function!
- JavaScript主要对象测试代码
- c#和Javascript操作同一json对象的实现代码
- [js点滴]JavaScript中Image对象操作实例
- 不安全的代码: 教你“随心所欲”地在内存中操作Java的类和对象(1)
- 【java神操作】java竟然还可以能执行Javascript代码!!
- 你真的了解jQuery的ready函数吗?当 DOM(文档对象模型) 已经加载,就执行ready里的代码。DOM何时加载完毕?jQuery对html局部操作,DOM为什么不重新加载?
- 不安全的代码: 教你“随心所欲”地在内存中操作Java的类和对象(2)
- vuex操作state对象的实例代码
- tensorboard可视化操作过程及测试代码
- JavaScript 操作对象属性(设置属性, setter/getter, 序列化)
- 全面总结Javascript对数组对象的各种操作
- javascript:面向对象和常见内置对象及操作