1

I would like to curry a function with different values from an object and save the created functions as object keys. The curried value gets overwritten while looping

function print (a,b){
    console.log(a + b)
}

var functions = {
    foo : "",
    bar : ""
}

for (var key in functions) {

    functions[key] = function(a){
        var val = key; // the problematic part saving the value
        return print(a,val)
    }

}

functions.foo('test-')
functions.bar('test-')

output

>>>test-bar
>>>test-bar

desired output

 >>>test-foo
 >>>test-bar

im missing the idea on how to save the val variable for good and not overwriting it while looping

Timar Ivo Batis
  • 1,861
  • 17
  • 21
  • 3
    Use `for(let key in functions)` instead of `for(var key in functions)`, that way you don't even need to do `var val = key;`, you can use `key` directly. – ibrahim mahrir Dec 12 '18 at 17:06

0 Answers0