JavaScript 对象链式操作测试代码

时间:2010-04-25    点击:142   
虽然现在慢慢减少了对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源代码还真是有点累。。
ImageZoom 图片放大镜效果(多功能扩展篇)
一些相见恨晚的 JavaScript 技巧
Javascript 中的 && 和 || 使用小结
js 禁用只读文本框获得焦点时的退格键
js 内存释放问题
> 返回     
地址:上海市普陀区胶州路941号长久商务中心 电话: QQ:
© Copyright 2012 上海网络 Product All Rights Reserved