2

I'm brushing up on knowledge of Javascript and filling in some of the gaps.

for the memoize function, I get why it would console log the function as the memoizeSomeFunction const is just a function expression of someFunction being passed into memoize as an arg. what I can't seem to conceptualize is how the arg being passed into memoizeSomeFunction gets to the return part of the function. can someone elaborate?

const memoize = (fn) => {
  console.log(fn);
  return (arg) => {
    console.log(arg)
  }
}

const someFunction = (x = 0, y = 0) => {
  return `x is {$x} y is ${y}`;
}

const memoizeSomeFunction = memoize(someFunction);
memoizeSomeFunction(1)

0

2 Answers 2

1

The memoize() function returns a function:

(...args) => {
    console.log(...args)
}

That function collects its arguments into an array using the spread (...) syntax, and then un-collects them the same way in the console.log() call.

Thus when you call the function returned from memoize(), it simply logs the arguments passed.

To summarize:

  • Just one argument is passed to memoize(), your someFunction() function;
  • The memoize() function returns another function, and that function logs its arguments (and otherwise does nothing with someFunction());
  • The function source is logged when memoize() is called, and the argument list to the return value from that is logged whenever it is called. If you added another call to that returned function, you'd see those arguments logged separately.
Sign up to request clarification or add additional context in comments.

2 Comments

how does the arg or args passed into memoizeSomeFunction get to the return piece of memoize in the first place?
Because the "return piece" is a function; they get to that function the same way arguments get to any function: via the parameter list in the function call. In the code you posted, the call is memoizeSomeFunction(1, 15), so the parameter list is 1, 15.
1

// ES5 equivalent:
const memoize_es5 = function(fn) {
  // fn is closured and can be used
  // function returned, when it will be called we will have call arguments as well
  return function () {
    console.log(fn.apply(null, arguments));
  }
}
const memoize = fn => (...args) => console.log(fn(...args));

const someFunction = (x = 0, y = 0) => {
  return `x is ${x} y is ${y}`;
}

const memoizeSomeFunction = memoize(someFunction); // function received as result of memoize call
memoizeSomeFunction(1, 15);                        // pass arguments to that function function

// ...args in the arguments is a rest operator
// it just capture all rest arguments into an array:
((first, ...args) => console.log(first, args))(1, 2, 3)
// ...args in a function call is spread operator
// it spreads an array into parameter list:
console.log(...['spread', 'operator'])

1 Comment

This is helpful but note that in the OP, the function returned from memoize() never references fn at all; it just logs the arguments.

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.