Home > Web Front-end > JS Tutorial > javascript forEach function implementation code_javascript skills

javascript forEach function implementation code_javascript skills

WBOY
Release: 2016-05-16 18:37:05
Original
1570 people have browsed it
Copy code The code is as follows:

function forEach(object, block, context, fn) {
if (object == null) return;
if (!fn) {
if (typeof object == "function" && object.call) {
//Traverse ordinary 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 (such as Array object of standard browser)
object.forEach(block, context);
return;
} else if (typeof object.length == "number") {
// If it is a class Array object or 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 here is always 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 from the original author (I jumped over the wall! ):
Copy code 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)
});


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh 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";//Extremely late binding
fred.wife = "Wilma";//Extremely late binding
forEach(fred,print)


[Ctrl A select all Note:
If you need to introduce external Js, you need to refresh to execute <script> function forEach(object, block, context, fn) { if (object == null) return; if (!fn) { if (typeof object == "function" && object.call) { //遍历普通对象 fn = Function; } else if (typeof object.forEach == "function" && object.forEach != arguments.callee) { //如果目标已经实现了forEach方法,则使用它自己的forEach方法(如标准游览器的Array对象) object.forEach(block, context); return; } else if (typeof object.length == "number") { // 如果是类数组对象或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) { // 这里的fn恒为Function for (var key in object) { //只遍历本地属性 if (object.hasOwnProperty(key)) { //相当于 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 ("司徒正美", print); forEach(document.styleSheets,function(el){ if(el.href) alert(el.href) }); </script>]
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template