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.

5
  • Use join? join(',') Or am I really misunderstanding the question? Commented Apr 24, 2015 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 developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Apr 24, 2015 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. Commented Apr 24, 2015 at 16:38
  • @forgivenson Yes, I think you might be right. It seems like the only way to implement this is using eval(). Thanks! Commented Apr 24, 2015 at 16:46
  • nvm, I tried it and eval will return the result of the comma operator, so in your example it returns 4. Commented Apr 24, 2015 at 17:00

4 Answers 4

2

Try the following:

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

Sign up to request clarification or add additional context in comments.

1 Comment

Hey, I think I did not communicate my question correctly. I just updated my question
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.

Comments

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]));

1 Comment

That was only an example to demonstrate expected input and output. I require a more generalized solution.
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'?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.