0

I'm trying to recreate the functionality of the underscore _.invoke for learning purposes and I would like to really understand how it works as it seems to be something not too complicated.

The exercise is asking me to return an array with the result of calling "a" method to it. Ok, so here we start.

_.invoke = function (collection, methodName) {
  let result = []; 
  // debugger;
  if (Array.isArray(collection)) { // check if collection is an array.
    for (let i = 0; i < collection.length; i++) { // iterate over collection
      result.push(Array.prototype.methodName.call(collection[i]));
    }
  }
  console.log('result:', result);
  return result;
};
I don't know exactly what method is being past to methodName nor if it has any extra arguments to be forwarded (this I understand it would be used in case I'd use a method that requires args like .reduce for instance if I'm not wrong).

As I understand, when I use the .call method on methodName, it should return (push) the iterated element with the "function" applied onto it. Obviously there is something not right, I have used the debugger to see what it does on each step and once it runs the loop and arrives to the call, it quits the loop and runs to check whatever it is it does in the config file of the test.

I get this message in the error log of the HTML file:

_.invoke(mocks.arr, 'testCall').should.eql(mocks.arr);
_.invoke(mocks.obj, 'testCall').should.eql(mocks.objValuesArr);

argsArr = [mocks.arr, mocks.obj];
_.invoke(mocks.arr, 'testArgs', mocks.arr, mocks.obj);
called.should.be.true;
called = false;
argsArr = [mocks.obj, mocks.arr];
_.invoke(mocks.obj, 'testArgs', mocks.obj, mocks.arr);
called.should.be.true;

The this, thisArg and such are still a little hard for me to understand, can someone explain to me what am I missing here..?

7
  • What does testCall refer to? Commented Oct 4, 2022 at 12:41
  • To clarify, are you trying to write your own implementation of lodash's invoke? lodash.com/docs/3.10.1#invoke Commented Oct 4, 2022 at 12:42
  • See stackoverflow.com/questions/28888777/… Commented Oct 4, 2022 at 12:43
  • @lucasvw yes and not exactly, i'm a JS student and I should learn the ins and outs of how things are made, which is where I'm struggling to understand the how. Commented Oct 4, 2022 at 14:28
  • @jarmod thanks for the link, I have already tried that with no success, there is something else that I'm not grasping Commented Oct 4, 2022 at 14:41

2 Answers 2

0

So, after some digging, trial and error, I was totally wrong about my approach to the exercise, so I had to re-make the whole thing.

_.invoke = function (collection, methodName) {
  // Spread all arguments into a variable.
  let args = [...arguments];
  // Since the arguments have been all passed to args, we don't need to call them as we normally would.
  // Use an already defined function (_.map) with an iteratee to be passed as method.
  return _.map(args[0], function (value) {
    // Return the iterated value passed through the function of _.map
    // and apply the rest of arguments to the element with the function from _.map if there are any.
    return value[args[1]].apply(value, args.slice(2));
  });
};

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

Comments

-1

I don't know much about underscore.js, but I'm pretty sure _ isn't defined at all, so maybe do window._.invoke = ... instead to properly define it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.