制作我自己的ForEach()javascript - 元素未定义

Jon*_*bek 4 javascript foreach

我在javascript中自己实现forEach,其唯一目的是更好地理解语言.更具体地说,临时目标是更好地理解回调.

这是我到底有多远,直到我卡住了.

function myForEach(array, callback) {
  for (let i = 0; i < this.length; i++) {
    callback(array[i]);
  }
}

function callback(element) {
  console.log(element); //insert logic
}

const array = [2, 4, 6, 8, 10];
arr.myForEach(array, callback(element));
Run Code Online (Sandbox Code Playgroud)

在节点中运行时出现以下错误:

ReferenceError: element is not defined
    at Object.<anonymous> (C:\Users\Jonas\Desktop\FLEXBOX\test.js:54:31)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Function.Module.runMain (module.js:694:10)
    at startup (bootstrap_node.js:204:16)
    at bootstrap_node.js:625:3
Run Code Online (Sandbox Code Playgroud)

我怀疑这是因为在调用函数时,不会创建在回调函数中作为参数给出的元素.这是有道理的,但是调用时真正的forEach循环不会传入已创建的值吗?

arr.forEach((element /*does not exist yet?*/) => {
    console.log(element);
});
Run Code Online (Sandbox Code Playgroud)

我也试过用lambda调用方法,但也没有得到正确的结果.但是一个不同的错误

arr.myForEach(array, (element) => {
    console.log(element);
});
Run Code Online (Sandbox Code Playgroud)

然后它给出错误:

TypeError: arr.myForEach is not a function
    at Object.<anonymous> (C:\Users\Jonas\Desktop\FLEXBOX\test.js:58:5)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Function.Module.runMain (module.js:694:10)
    at startup (bootstrap_node.js:204:16)
    at bootstrap_node.js:625:3
Run Code Online (Sandbox Code Playgroud)

ash*_*ngh 7

这就是你如何实现它

    Array.prototype.myForEach = function(callback) {
      for (let i = 0; i < this.length; i++) {
        callback(this[i]);
      }
    };
    
    function callback(element) {
      console.log(element); //insert logic
    }
    
    var array = [2, 4, 6, 8, 10];
    array.myForEach( callback);
Run Code Online (Sandbox Code Playgroud)

错误element not defined is because in last line you use 'callback(element)',因为没有定义该行元素

并且能够在数组上调用方法,就像array.myForEach你必须将它附加到原型一样

注意:不建议更改内置原型.谢谢你的评论

  • 因为您正在使用`Array.prototype`,所以您可能希望删除对`array`的引用并仅使用`this` (2认同)

Dam*_*lta 4

您有几个错误,请检查以下内容:

function myForEach(array, callback) {
    for (let i = 0; i < array.length; i++) { 
        callback(array[i]);
    }
}

function callback(element) {
    console.log(element); //insert logic
}

const array = [2, 4, 6, 8, 10];
myForEach(array, callback); 
Run Code Online (Sandbox Code Playgroud)

错误是(见评论):

function myForEach(array, callback) {
  for (let i = 0; i < this.length; i++) { // what is this? You need array.length
    callback(array[i]);
  }
}

function callback(element) {
  console.log(element); //insert logic
}

const array = [2, 4, 6, 8, 10];
arr.myForEach(array, callback(element)); 
// arr.myForEach is not possible, because myForEach is not a method of JS arrays 
// callback(element) this invokes a function, you just need to pass it like callback (without parentheses)
Run Code Online (Sandbox Code Playgroud)