Does forEach() bind by reference?

后端 未结 3 1302
长发绾君心
长发绾君心 2020-12-20 11:57
var arr = [\'Foo\'];

arr.forEach(function(item){
  console.log(item);
  item = \'Lorem\';
  console.dir(arr[0]);

});

for (var item in arr){
  arr[item] = \'Ipsum\         


        
3条回答
  •  失恋的感觉
    2020-12-20 12:45

    Notice that callback in forEach gets three arguments. The second one being an index of current element, and the third one the array itself. If you want to modify the array, use these two arguments.

提交回复
热议问题