Html5.js expands prototype of js to support html5 functions in browsers that do not support html5 (continuous addition)

Source: Internet
Author: User

Extended html5 array new api array added several classic traversal filtering methods, which are very convenient. This prevents the use of the for loop in the transition. The prototype extension of javascript objects provides a unified front-end call interface. The Code is as follows: copy the code (function (_ window) {// some methods are added to the array // The forEach method traverses the array from start to end, and the specified function is called for each array element to provide the current element, the current index and Array are passed to the Function Array as parameters. prototype. forEach | (Array. prototype. forEach = function (fun) {for (var I = 0; I <this. length; I ++) {fun (this [I], I, this) ;}} // The map Method traverses the array and calls the specified function. Finally, an array is returned, an Array consists of all function return values. prototype. map | (Array. prototype. map = function (fun) {var returnArray = []; for (var I = 0; I <this. length; I ++) {returnArray. push (fun (this [I], I, this);} return returnArray;}) // filter is a handsome selector of an array. it traverses the array, calls the specified function, and returns a new array, if the function returns true, the returned array is added. Array. prototype. filter | (Array. prototype. filter = function (fun) {var returnArray = []; for (var I = 0; I <this. length; I ++) {if (fun (this [I], I, this) {returnArray. push (this [I]);} return returnArray;}) // calls the specified function to traverse the array. If all functions return true, every returns true; otherwise, false. Array. prototype. every | (Array. prototype. every = function (fun) {if (this. length = 0) return true; // every should end the loop as soon as possible for (var I = 0; I <this. length; I ++) {if (! Fun (this [I], I, this) {return false ;}return true ;}) // some traversal array calls the specified function, if a function returns true, true is returned. If all functions return false, false Array is returned. prototype. some | (Array. prototype. some = function (fun) {if (this. length = 0) return false; // some should end the loop as soon as possible for (var I = 0; I <this. length; I ++) {if (fun (this [I], I, this) {return true ;}} return false ;}) // The reduce traversal array calls the specified function (the function requires two parameters). Each time the return value and the next element of the called function are used as the function parameters to continue calling. // The Array summation operation is very useful. prototype. reduce | (Array. prototype. reduce = function (fun, initval) {if (this. length = 0) return ""; if (this. length = 1) {return initval? Fun (initval, this [0]): this [0];} var val = initval, firstindex = initval? 0: 2; if (! Initval) {val = fun (this [0], this [1]) ;}for (var I = firstindex; I <this. length; I ++) {val = fun (val, this [I]);} return val ;}) // same as reduce, reduceRight only traverses the Array from right to left. prototype. reduceRight | (Array. prototype. reduceRight = function (fun, initval) {if (this. length = 0) return ""; if (this. length = 1) {return initval? Fun (initval, this [0]): this [0];} var val = initval, firstindex = initval? This. length-1: this. length-3; if (! Initval) {val = fun (this [this. length-1], this [this. length-2]) ;}for (var I = firstindex; I> = 0; I --) {val = fun (val, this [I]);} return val ;})} (window );

Related Article

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.