javascript利用apply和arguments复用方法
时间:2024-04-16 09:13:19
首先,有个单例对象,它上面挂了很多静态工具方法。其中有一个是each,用来遍历数组或对象。
var nativeForEach = [].forEach
var nativeMap = [].map
var util = {
each: function (obj, iterator, context) {
if (obj == null) return
if (nativeForEach && obj.forEach === nativeForEach) {
obj.forEach(iterator, context)
} else if ( obj.length === +obj.length ) {
for (var i = 0; i < obj.length; i++) {
if (iterator.call(obj[i] || context, obj[i], i, obj) === true) return
}
} else {
for (var k in obj) {
if (iterator.call(obj[k] || context, obj[k], k, obj) === true) return
}
}
},
map: function(obj, iterator, context) {
var results = []
if (obj == null) return results
if (nativeMap && obj.map === nativeMap) return obj.map(iterator, context)
this.each(obj, function(val, i, coll) {
results[i] = iterator.call(context, val, i, coll)
})
return results
}
}
还有诸如every、some等对集合(Array,Hash)操作的工具函数。使用时采用util.xx方式。
如果定义了一个集合类,这个类内部有集合数据。
function Collection(data) {
this.data = data || []
// some other property
// this.xxx = yyy
}
Collection.prototype = {
// some method
}
可以很方便的把util上的方法拷贝到集合类上,如
function copyMethod(clazz, obj) {
for (var method in obj) {
clazz.prototype[method] = function() {
var args = [].slice.call(arguments)
var target = this.data
args.unshift(target)
obj[method].apply(obj, args)
}
}
}
copyMethod(Collection, util)
这样拷贝后,Collection的实例就有了util上的方法,util操作的集合对象(第一个参数)就是Collection的this.data。如下直接可以遍历this.data了。
var coll = new Collection([10, 20, 30])
// 遍历
coll.each(function(k) {
console.log(k)
})
// 操作
var arr = coll.map(function(k) {
return k - 5
})
console.log(arr) // 5, 15, 25
这种模式在很多开源库中使用,比如jQuery,它的 $.each/$.map 很方便的拷贝到了 $().each/$().map。
又如Backbone,它的 _.each/_.map/_.every/_.chain (还有很多)都拷贝到了 Collection的原型上。
// Underscore methods that we want to implement on the Collection.
// 90% of the core usefulness of Backbone Collections is actually implemented
// right here:
var methods = ['forEach', 'each', 'map', 'collect', 'reduce', 'foldl',
'inject', 'reduceRight', 'foldr', 'find', 'detect', 'filter', 'select',
'reject', 'every', 'all', 'some', 'any', 'include', 'contains', 'invoke',
'max', 'min', 'toArray', 'size', 'first', 'head', 'take', 'initial', 'rest',
'tail', 'drop', 'last', 'without', 'difference', 'indexOf', 'shuffle',
'lastIndexOf', 'isEmpty', 'chain'];
// Mix in each Underscore method as a proxy to `Collection#models`.
_.each(methods, function(method) {
Collection.prototype[method] = function() {
var args = slice.call(arguments);
args.unshift(this.models);
return _[method].apply(_, args);
};
});
又有,把 _.keys / _.values / _.pairs / _.invert / _.pick 等对对象操作的实用方法拷贝了 Backbone.Model上 (1.0新增)
var modelMethods = ['keys', 'values', 'pairs', 'invert', 'pick', 'omit'];
// Mix in each Underscore method as a proxy to `Model#attributes`.
_.each(modelMethods, function(method) {
Model.prototype[method] = function() {
var args = slice.call(arguments);
args.unshift(this.attributes);
return _[method].apply(_, args);
};
});
标签:javascript,apply,arguments
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
在Python中执行cmd
2022-05-20 07:24:50
Python中Continue语句的用法的举例详解
2023-04-04 13:12:37
![](https://img.aspxhome.com/file/2023/2/105612_0s.jpg)
Django如何实现RBAC权限管理
2021-05-20 19:14:27
![](https://img.aspxhome.com/file/2023/1/103361_0s.png)
深入透析样式表滤镜(下)
2011-06-14 09:49:19
统一的品牌设计
2008-08-07 12:53:00
![](https://img.aspxhome.com/file/UploadPic/20088/7/200887125533323s.jpg)
赚疯了!转手立赚800+?大佬的python「抢茅台脚本」使用教程 <font color=red>原创</font>
2022-03-02 04:40:53
![](https://img.aspxhome.com/file/2023/1/135021_0s.jpg)
javascript获取wx.config内部字段解决微信分享
2024-04-23 09:15:50
![](https://img.aspxhome.com/file/2023/1/135661_0s.jpg)
关于vue3 vuex4 store的响应式取值问题解决
2024-05-02 16:32:55
pytorch 把图片数据转化成tensor的操作
2022-01-14 19:02:46
![](https://img.aspxhome.com/file/2023/6/118106_0s.jpg)
Mootools常用方法扩展(一)
2009-01-09 12:45:00
Python中的super用法详解
2022-06-29 16:43:04
详谈python http长连接客户端
2023-07-30 22:37:01
python 实现有道翻译功能
2022-03-25 17:30:55
![](https://img.aspxhome.com/file/2023/3/135183_0s.png)
Python Pandas中缺失值NaN的判断,删除及替换
2021-08-27 22:10:23
浅谈品牌的视觉识别
2009-07-03 12:28:00
![](https://img.aspxhome.com/file/UploadPic/20097/3/00-65s.jpg)
Go语言入门exec的基本使用示例
2024-04-25 13:16:33
在django中使用post方法时,需要增加csrftoken的例子
2023-08-12 06:44:34
Python requests的SSL证书验证方式
2021-10-06 09:21:51
![](https://img.aspxhome.com/file/2023/8/118058_0s.png)
Python Socket传输文件示例
2023-10-18 17:19:00
Python的collections模块中namedtuple结构使用示例
2022-06-01 19:59:42