0

考虑这个例子:

var a = {
    "Check" : function doThis(value, index, ar) { 
        if(value >= 10) {
            document.write(value + ", ");
        }
        else {
            document.write("The array element is less than 10, ");
        }
    }       
}

var arr = [12, 20, 2, 3, 15];
document.write(arr.forEach(a.Check)); 

结果是:

12, 20, The array element is less than 10, 
The array element is less than 10, 15, undefined

我不明白为什么数组中有一个未定义的额外元素。它与在对象中定义回调函数有关吗?

4

1 回答 1

2

代替:

document.write(arr.forEach(a.Check)); 

和:

arr.forEach(a.Check); 

document.write(arr.forEach(a.Check));您一起打印arr.forEach()调用返回的内容 ( undefined)

于 2013-09-01T22:46:26.820 回答