>var arr = ["ab","cd","ef"]
>var show = function(value,index,ar){ print(value) }
>arr.forEach(show)
ab
cd
ef
//value为当前元素值,index为当前元素在数组中的索引,ar为数组本身
functionShowResults(value, index, ar) {
document.write("value:" + value);
document.write("index: " + index);
document.write("name: " + this.name);
document.write("
");
}
varletters = ['ab', 'cd', 'ef'];
varscope = { name: 'scope' };
// ShowResults为回调函数,scope为回调设置上下文环境,使回调函数的this指向scope变量,scope为可选参数
letters.forEach(ShowResults,scope);