Javascript foreach Function

Source: Internet
Author: User

finding a foreach function in base2 is the best implementation I have ever seen. Dig out and analyze it. It can traverse various common objects, strings, arrays, and arrays of classes. If the object of the original browser has implemented this function, it calls the function of the original object.

Function foreach (object, block, context, FN) {If (Object = NULL) return; If (! FN) {If (typeof object = "function" & object. call) {// traverse Common Object fn = function;} else if (typeof object. foreach = "function" & object. foreach! = Arguments. callee) {// if the target has implemented the foreach method, use its own foreach method (such as the array object of the standard browser) object. foreach (Block, context); return;} else if (typeof object. length = "Number") {// if it is an array object of the class or an array object of IE _ array_foreach (object, block, context); Return ;}} _ function_foreach (FN | object, object, block, context) ;}; function _ array_foreach (array, block, context) {If (array = NULL) return; vaR I = 0, length = array. length; If (typeof array = "string") {for (; I <length; I ++) {block. call (context, array. charat (I), I, array) ;}} else {for (; I <length; I ++) {block. call (context, array [I], I, array) ;}}; _ function_foreach = function (FN, object, block, context) {// The FN constant here is function for (var key in object) {// only traverses the local attribute if (object. hasownproperty (key) {// equivalent to block (object [Key], key) block. call (context, object [Key], key, object );}}};

Some examples of the original author (I crossed the wall !) :

 
Function print (El, index) {alert (index + ":" + El)} foreach ([1, 2, 3], print); foreach ({: "AA", B: "BB", C: "cc"}, print); foreach ("situ zhengmei", print); foreach (document. stylesheets, function (EL) {If (El. href) Alert (El. href )});

<Br/> function foreach (object, block, context, FN) {<br/> If (Object = NULL) return; <br/> If (! FN) {<br/> If (typeof object = "function" & object. call) {<br/> // traverse common objects <br/> fn = function; <br/>} else if (typeof object. foreach = "function" & object. foreach! = Arguments. callee) {<br/> // if the target has implemented the foreach method, use its own foreach method (such as the array object of the standard browser) <br/> object. foreach (Block, context); <br/> return; <br/>} else if (typeof object. length = "Number") {<br/> // if it is an array object of the class or an array object of IE <br/> _ array_foreach (object, block, context ); <br/> return; <br/>}< br/> _ function_foreach (FN | object, object, block, context ); <br/>}; </P> <p> function _ array_foreach (array, BL Ock, context) {<br/> If (array = NULL) return; <br/> var I = 0, length = array. length; <br/> If (typeof array = "string") <br/> array = array. split (""); <br/> (; I </P> <p> <button type = "button" Title = "runcode0" class = "runcode direct"> RUN <SPAN class = 'wp _ keywordlink '> Code </span> </button> </P> <PRE class = "Brush: javascript; toolbar: false; gutter: false "> function person (name, age) {This. name = Name | ""; This. age = age | 0 ;}; var Fred = new person ("Fred", 38); Fred. language = "English"; // binds Fred very late. wife = "Wilma"; // It is very late to bind foreach (Fred, print) </PRE> <p> <textarea id = "runcode1" style = "width: 80% "rows =" 10 "> <br/> function foreach (object, block, context, FN) {<br/> If (Object = NULL) return; <br/> If (! FN) {<br/> If (typeof object = "function" & object. call) {<br/> // traverse common objects <br/> fn = function; <br/>} else if (typeof object. foreach = "function" & object. foreach! = Arguments. callee) {<br/> // if the target has implemented the foreach method, use its own foreach method (such as the array object of the standard browser) <br/> object. foreach (Block, context); <br/> return; <br/>} else if (typeof object. length = "Number") {<br/> // if it is an array object of the class or an array object of IE <br/> _ array_foreach (object, block, context ); <br/> return; <br/>}< br/> _ function_foreach (FN | object, object, block, context ); <br/>}; </P> <p> function _ array_foreach (array, BL Ock, context) {<br/> If (array = NULL) return; <br/> var I = 0, length = array. length; <br/> If (typeof array = "string") <br/> array = array. split (""); <br/> (; I </P> <p> <button type = "button" Title = "runcode1" class = "runcode direct"> run the Code </button> </P> <p> in base2, a method called unbind is also provided, we can use it to strip the foreach method of the native object for our call: </P> <PRE class = "Brush: javascript; toolbar: false; gutter: false "> VaR _ slice = array. protot Ype. slice; function unbind (FN) {return function (context) {return fn. apply (context, _ slice. call (arguments, 1) ;};</PRE> <p> <textarea id = "runcode2" style = "width: 80% "rows =" 10 "> <br/> try {<br/> VaR _ slice = array. prototype. slice; <br/> function unbind (FN) {<br/> return function (context) {<br/> return fn. apply (context, _ slice. call (arguments, 1); <br/>}; <br/> function print (El, index) {<Br/> alert (index + ":" + El) <br/>}< br/> var each = unbind (array. prototype ["foreach"]) <br/> var A = {CC: function (e) {alert (e) }}; <br/> each (["11111 ", & quot; 22222 & quot;],. CC, A) // The final A is dispensable <br/>} catch (e) {alert ("Please use it in a standard browser! ")} <Br/>

Run code

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.