1

I am attempting to reimplement apply for practice. I figured out a naive solution to execute a function in the context of an object by temporarily decorating that function on the object and then deleting it after. But I can't figure out how to pass the fn comma separated arguments when I invoke it because they are in array form.

How can I transform an array into a comma separated variables WITHOUT apply, call, or bind?

function apply(fn, context, args) {
  context.fn = fn;
  var result = context.fn();
  delete context.fn;
  return result;
}

function add(a,b) {
  return a + b + this.num;
}

console.log(apply(add, {num: 10}, [3, 4])); //17

EDIT:

I do NOT want my values split into a string. I want my values split into comma separated form. This is essentially what apply does under the hood when you pass it an array.

chopper draw lion4
  • 12,401
  • 13
  • 53
  • 100
  • Use join? `join(',')` Or am I really misunderstanding the question? – forgivenson Apr 24 '15 at 16:30
  • Sorry, there is a miscommunication here. I do not want a string. I want my values to be separated with the comma operator https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator – chopper draw lion4 Apr 24 '15 at 16:35
  • Well, you didn't say not to use `eval`, so you could use `eval` on the string produced by `join`, but that is best to avoid in general. However, your use case sounds like a special case, as you are recreating a built-in function. – forgivenson Apr 24 '15 at 16:38
  • @forgivenson Yes, I think you might be right. It seems like the only way to implement this is using eval(). Thanks! – chopper draw lion4 Apr 24 '15 at 16:46
  • nvm, I tried it and `eval` will return the result of the comma operator, so in your example it returns `4`. – forgivenson Apr 24 '15 at 17:00

4 Answers4

2

Try the following:

[1,2,3,4,5].join(",")

Adrian Lynch
  • 8,237
  • 2
  • 32
  • 40
1

You can do so in ES6 with the help of the spread operator:

console.log(...myArray);

At the time of this writing, this feature is supported by FF37 and transpilers such as Traceur and Babel.

Another way of doing it would be by currying the function and looping to pass the arguments. Something along the lines of:

myArray.forEach(curriedConsoleLog);

That said, I don't see harm in just using apply.

Gerson Goulart
  • 584
  • 6
  • 14
0

As your function can only receive two arguments this would work :

function apply(fn, context, args) {
  context.fn = fn;
  var result = context.fn(args[0],args[1]);
  delete context.fn;
  return result;
}

function add(a,b) {
  return a + b + this.num;
}

console.log(apply(add, {num: 10}, [3, 4]));
0
function apply(fn, context, args) {
    context.fn = fn;
    while (args.length) {
        context.fn = context.fn(args.shift());
    }
    return context.fn;
}

function cubus(a) {
    return function (b) {
        return function (c) {
            return a * b * c / this.num;
        }
    }
}

function add(a) {
    return function (b) {
        return a + b + this.num;
    }
}

document.write(apply(add, { num: 10 }, [3, 4]));
document.write('<br>');
document.write(apply(cubus, { num: 10 }, [3, 4, 5]));

have a look at What is 'Currying'?

Community
  • 1
  • 1
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392