Well, start reading the source code of Zepto, the first to deal with the original implementation of trim and reduce, the sense of writing is very compact, which reduce write a little obscure, personal feeling is good. The author of the main zepto is a non-colon party that looks a bit unaccustomed.
3 if (String.prototype.trim = = = undefined)//fix for IOS 3.2 4 String.prototype.trim = function () {5 return This.replace (/^\s+|\s+$/g, ")//Trim function similar to PHP 6} 7 8//For IOS 3.x 9//from Https://developer.mozilla.or G/en/javascript/reference/global_objects/array/reduce 10//The function of this method is to be tired like a cumulative processing effect, the previous data processing results used as the next processing 11//For example [ 1,2,3,4,].reduce (function (x, y) {return x+y}); ==> ((1+2) +3) +4, if (Array.prototype.reduce = = = undefined) Array.prototype.reduce = function (fun) {14 if (this = = = void 0 | | this = = NULL) throw new TypeError () var t = Object (this),//t is the copy of the array itself this is Len = T.length >>> 0,//array length k = 0,//array subscript variable accumulator//the variable that holds the result if (typeof fun! = ' function ') throw new TypeError () if (len = = 0 && arguments.length = 1) throw new TypeError () 21//Take initial value 22 if (arguments.length >= 2) accumulator = arguments[1]//If the parameter length is greater than 2, the second parameter is the initial value of4 if (k in t) {accumulator = t[k++]//Otherwise the first data of the array is used as the initial value of break (++k >= len) throw new TypeError ()//Under what circumstances will it be executed here??? while (true) 30//iterates over the array, the previous result is passed to the processing function for cumulative processing (K < Len) {32if (k in t) accumulator = fun.call (undefined, accumulator, t[k], K, T)//CORE operation codek++ Accumulator 36}
Zepto Learning Path--array de-weight and native reduce