Finding a function called forEach 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.
The Code is as follows:
Function forEach (object, block, context, fn ){
If (object = null) return;
If (! Fn ){
If (typeof object = "function" & object. call ){
// Traverse common objects
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 (for example, 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 ){
// Fn constant here is Function
For (var key in object ){
// Only traverse local properties
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 !) :
The Code is as follows:
Function print (el, index ){
Alert (index + ":" + el)
}
ForEach ([1, 2, 3], print );
ForEach ({a: "aa", B: "bb", c: "cc"}, print );
ForEach ("situ zhengmei", print );
ForEach (document. styleSheets, function (el ){
If (el. href) alert (el. href)
});
Script 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") array = array. split (""); 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) ;}}; function print (el, index) {alert (index + ":" + el )} forEach ([1, 2, 3], print); forEach ({a: "aa", B: "bb", c: "cc"}, print ); forEach ("situ zhengmei", print); forEach (document. styleSheets, function (el) {if (el. href) alert (el. href)}); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
The Code is as follows:
Function Person (name, age ){
This. name = name | "";
This. age = age | 0;
};
Person. prototype = new Person;
Var fred = new Person ("Fred", 38 );
Fred. language = "English"; // very late binding
Fred. wife = "Wilma"; // very late binding
ForEach (fred, print)
<Script type = "text/javascript"> 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") array = array. split (""); 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) ;}}; function print (el, index) {alert (index + ":" + el )} function Person (name, age) {this. name = name | ""; this. age = age | 0;}; Person. prototype = new Person; var fred = new Person ("Fred", 38); fred. language = "English"; // binds fred very late. wife = "Wilma"; // binds forEach (fred, print) script very late
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]