代码之家  ›  专栏  ›  技术社区  ›  Steve Bennett

映射函数名失败,内联函数成功

  •  1
  • Steve Bennett  · 技术社区  · 7 年前

    const loadImage = thenify(map.loadImage).bind(map);
    const markerNames = ['markers/red-map-marker.png', 'markers/teal-map-marker.png'];
    
    // works
    markerNames.map(x => loadImage(x))                      
    
    // works
    markerNames.map(function(x) { return loadImage(x) })   
    
    // fails: "TypeError: e is not a function"
    markerNames.map(loadImage)
    

    我很难理解为什么第三种形式会失败。我以为它的意思和第二种形式一样。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Steve Bennett    7 年前

    我想是因为 Array.map() 将两个参数传递给函数-数组元素,然后是数组索引。第二个是被解释为它应该是一个函数,而不是。