A graceful JS code

Source: Internet
Author: User

<!--———————————————— JS prototype (prototype) ————————————————————-->
//implement array each (loop array for each item)
var arr=[1,3,4,2];
Arr.foreach (function (item,index,array) {
//alert (item);
});
var arr2=[1,3,23,[12,[13,[45]];
array.prototype.each=function (FN) {
//destination iterates over each item of an array
try{
//Counter records the position of the currently traversed element
this.i| | (this.i=0);
//Judge when to enter
if (this.length>0&&fn.constructor==function) {
//Iterate through each item of the array
While (this.i<this.length) {
//Get an array of each item
var e=this[this.i];
//Judgment element exists and is an array
if (e&&e.constructor==array) {
E.each (FN);
}else{
//How to use bindings
//fn.apply (E,[e]);
Fn.call (e,e);
}
this.i++;
}
this.i=null;//free Memory
}
}catch (ex) {
}
}
Arr2.each (function (item) {alert (item);});

A graceful JS code

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.