>var arr = ["ab","cd","ef"]
>var show = function(value,index,ar){ print(value) }
>arr.forEach(show)
ab
cd
ef
//value為當前元素值,index為當前元素在數(shù)組中的索引,ar為數(shù)組本身
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為回調(diào)函數(shù),scope為回調(diào)設置上下文環(huán)境,使回調(diào)函數(shù)的this指向scope變量,scope為可選參數(shù)
letters.forEach(ShowResults,scope);