jQuery源码浅谈系列---$.map
?
jQuery源码浅谈系列---$.map
?
其实这个api的实现原理上会很类似我以前写的$.each的一些思想。如果你弄懂了前面这个,我想再来看看$.map可能就很容易理解了
?
-------------功能上针对的是Array,将一个Array的元素转换到另一个数组中
?
ps:昨天和光哥探讨一些技术的时候忽然光哥问我 你觉得map到底是什么? thinking...............
?
?
先上源码:
?
?
/**map-traverse the array with the func into an new array**@param {Array} source---the array**@param {Function} iterator---the func**@param {Object} thisObj**@remark(if thisObj is not defined,default the source)**@return {Array} results----after map**/ZYC.array.map = function(source,iterator,thisObj){ var results = [], i=0, _length =source.length;for(;i<_length;i++){ //这边我的函数调用方式是call,支持了第3个参数thisObj //如果thisObj没有定义的话就直接拿source //思想参照了tangram //没有对返回值进行null的判断直接装 results[i] = iterator.call(thisObj ||source, source[i],i);}return results;};??
?
?
?
?