Zepto Learning Path--array de-weight and native reduce

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.