Copy Code code as follows:
function ForEach (object, block, context, FN) {
if (object = = null) return;
if (!FN) {
if (typeof object = = "function" && object.call) {
Traverse Normal Objects
fn = Function;
else if (typeof Object.foreach = = "function" && Object.foreach!= Arguments.callee) {
If the target has implemented the Foreach method, it uses its own foreach method (for example, a standard browser array object)
Object.foreach (blocks, context);
Return
else if (typeof object.length = = "Number") {
If it is a class array object 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 here is a function.
for (var key in object) {
Traverse Local Properties only
if (Object.hasownproperty (key)) {
Equivalent to block (Object[key], key)
Block.call (context, Object[key], key, object);
}
}
};
Some examples of the original author (I pulled over the wall!) ):
Copy Code code as follows:
function Print (El,index) {
Alert (index+ ":" +el)
}
ForEach ([1, 2, 3], print);
ForEach ({A: "AA", B: "BB", C: "CC"}, print);
ForEach ("Masaki", 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 normal object fn = function; else if (typeof Object.foreach = = "function" && Object.foreach!= Arguments.callee) {//If the target has implemented the ForEach method, use it Own ForEach method (such as the standard Browser array object) Object.foreach (block, context); Return else if (typeof object.length = = "Number") {//If it is an array object of an array object or 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) {///Here the FN constant is Function for (var key in object) {///only traverse local property 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 ("Masaki", print); ForEach (Document.stylesheets,function (EL) {if (el.href) alert (EL.HREF)}); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
Copy Code code 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 = "Chinese";//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 normal object fn = function; else if (typeof Object.foreach = = "function" && Object.foreach!= Arguments.callee) {//If the target has implemented the ForEach method, use it Own ForEach method (such as the standard Browser array object) Object.foreach (block, context); Return else if (typeof object.length = = "Number") {//If it is an array object of an array object or 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) {///Here the FN constant is Function for (var key in object) {///only traverse local property 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 = "中文版"; very late bound fred.wife = "Wilma";//Very late bind ForEach (fred,print) </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]