0

I am trying to create a generic function where you are able to pass an object that has a property of a random function. With this you should be able to set a property stating the key and value for each parameter of the function.

the generic function should then call this "random" function with all the parameters.

However im not quite sure how to do it?

// Keep in mind its created for demonstration purposes

var functionOne = function(id)
{
    return id;
}
var functionTwo = function(id,name)
{
    return id + ' 'name;
}

var functionThree = funciton(id,name,age)
{
        return id + ' '+name+' '+age;
}
var obj = [
  {
      callback: functionOne,
      callbackParameters: [{key: 'id', value: 1}]

  },
  {
      callback: functionTwo,
      callbackParameters: [{key: 'id', value: 1}, {key: 'name', value:'Marc'}]

  },

  {
      callback: functionThree,
      callbackParameters: [{key: 'id', value: 1}, {key: 'name', value: 'Marc'}, {key: 'age', value: 45}]

  }
]

obj.forEach(function(x){
    //How do i call it with the correct keys? :(
})

Fiddle

Marc Rasmussen
  • 19,771
  • 79
  • 203
  • 364

2 Answers2

4

You can call apply() on a function in JS and pass an array of parameters into it. So you could use the following assuming your callbackParameters are always in the correct order.

obj.forEach(function(x){
    var parameters = x.callbackParameters.map(function(p) { return p.value; });
  console.log(x.callback.apply(this, parameters));
})

Updated fiddle https://jsfiddle.net/y6oh1078/1/

Edit: Further reading

If you are interested in more ways to manipulate functions in JS, the following article on currying is a good read - https://www.sitepoint.com/currying-in-functional-javascript/

Joe
  • 1,847
  • 2
  • 17
  • 26
1

You cannot do this. This would require something like reflection, i.e. forEach anonymous function should know the definition of every callback function and get names (and what is much more important - order) of its arguments.

However, you can do the following:

var functionOne = function (o) {
  return o.id;
};
var functionTwo = function (o) {
  return o.id + ' ' + o.name;
};
var functionThree = function (o) {
  return o.id + ' ' + o.name + ' ' + o.age;
};

var obj = [{
    callback : functionOne,
    callbackParameters : [{
        key : 'id',
        value : 1
      }
    ]
  }, {
    callback : functionTwo,
    callbackParameters : [{
        key : 'id',
        value : 1
      }, {
        key : 'name',
        value : 'Marc'
      }
    ]
  }, {
    callback : functionThree,
    callbackParameters : [{
        key : 'id',
        value : 1
      }, {
        key : 'name',
        value : 'Marc'
      }, {
        key : 'age',
        value : 45
      }
    ]
  }
];

// Now, are you able to generate `o` objects dynamically using something like:
obj.forEach(function (x) {
  var o = {};
  x.callbackParameters.forEach(function (p) {
    o[p.key] = p.value;
  });
  console.log(x.callback(o));
});

P.S. Actually, you can dynamically get the names and order of function arguments, but you do not want to do this.
If you are still interested in this, read this question.

Community
  • 1
  • 1
Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101