I want to store a function and its arguments in an object and invoke it via this object, as follows:
var fn = function(x, y) {
return x + y
};
var obj = {
'args': [2, 3],
'func': fn
};
console.log(obj.func(obj['args']));
Why does this code return a string "2,3undefined"? It's not even '23' if it receives the arguments as strings and + concats them. How do I make it return 5 as expected? Thanks a bunch!
[2,3], the second is undefined since you don't pass anything to it. The+operator is overloaded, it does addition, concatenation or type conversion to Number depending on the context.