2016-12-02 42 views
-1

我在JS測試高階函數,而是不斷收到錯誤 「地圖未定義」誰能幫助?地圖是未定義的錯誤 - 高階函數

function mapper(f) { 
    return function(a) { return map(a, f); }; 
} 

var increment = function(x) { return x+1; }; 
var incrementer = mapper(increment); 
incrementer([1,2,3]) 

回答

3

map不是全局函數,但a property of Array's prototype

function mapper(f) { 
 
    return function(a) { return a.map(f); }; 
 
} 
 

 
var increment = function(x) { return x+1; }; 
 
var incrementer = mapper(increment); 
 
var out = incrementer([1,2,3]) 
 

 
console.log(out); // [2,3,4]