0

I've the following situation: I want to prepend (unshift) the arguments given to an function by another parameter. How my current approach looks like:

function eventReferer(event) {
  var self = this;

  return function() {
    var args = ([event]).concat(Array.prototype.slice.call(arguments, 0, arguments.length));
    return eventFunction.apply(self, args);
  };
}

"eventFunction" is a custom function. I need to redirect the called event + all arguments to this function.

Because "arguments" is no valid Array in Javascript, the method of Array will not work. Is there any better way to merge my event and the arguments to a new array?

SuperNova
  • 2,792
  • 2
  • 25
  • 34
  • 1
    You can convert arguments to array as per latest ECMA standards. http://stackoverflow.com/questions/960866/converting-the-arguments-object-to-an-array-in-javascript http://stackoverflow.com/questions/1316371/converting-a-javascript-array-to-a-function-arguments-list – vivek_nk Apr 08 '14 at 10:50
  • Okay, that's exactly what I'm doing above. So there is no other way of doing this? – SuperNova Apr 08 '14 at 12:08
  • @vivek_nk: Did you answer the wrong Question? – SuperNova Apr 08 '14 at 12:38
  • ah. sorry. that was a terrible mistake. will remove it now – vivek_nk Apr 08 '14 at 12:39
  • However, I'd only inspected the accepted answer, but the other answers are going deeper into detail. I tried the testcase on http://jsperf.com/ghel-args and selected the best version for my environment. – SuperNova Apr 08 '14 at 12:40

1 Answers1

0

There are some more ways shown in the benchmark on http://jsperf.com/ghel-args - The only thing that's to do, is to unshift the "event" param to the array after converting the arguments to a regular JS-Array using one of the functions. So my first approach was correct, but not ideal (because it's not the fastest method available).

SuperNova
  • 2,792
  • 2
  • 25
  • 34