1

I came across this piece of code ... where args is interpreted to fn args object ... this is really confusing for me, and I couldn't get it

function memoize(fn) {
    return function(...args) {
        fn.apply(this, args);
    }
}

How does the inner function understand that args passed to it belongs to fn

Edit

Now I get it ... This wrapper function should be called this way const fastfunction = memoize(slowfunction); ... and the returned function (the inner-function) 'd be fastFunction which should be called with a set of args... and ...args is kinda a placeholder for these args

Hend El-Sahli
  • 6,268
  • 2
  • 25
  • 42
  • It doesn’t. It only applies the arguments given to it to fn – evolutionxbox Mar 09 '21 at 18:56
  • The inner function itself applies the variable `fn` to itself as an argument. The "applied to" is given as `this`, and the application is on `args`. – Endothermic_Dragon Mar 09 '21 at 18:58
  • This is the classical `memoize` wrapper that should be used this way ---> `const fastfunction = memoize(slowfunction);` ... and it successfully cal `fn` with its arguments ... and I have no idea how does that happen ? – Hend El-Sahli Mar 09 '21 at 19:06
  • Thank you so much guys ... appreciate ut ... this was really confusing ... – Hend El-Sahli Mar 09 '21 at 19:11
  • After `const fastfunction = memoize(slowfunction)`, the returned `fastfunction` (inner function) remembers what `fn` is because of how [lexical scoping](https://stackoverflow.com/questions/1047454/what-is-lexical-scope) works. When you then call `fastfunction`, the arguments are applied to `fn` (`slowfunction`) due to `fn.apply(this, args)`. Say the argumets are `1`, `2` and `3`. Then `fastfunction(1,2,3)` is passing those arguments to `fn` (which is set to `slowfunction`). So it is the same as `slowfunction(1,2,3)` – 3limin4t0r Mar 09 '21 at 19:26

1 Answers1

0

the this in this example has no effect since it is undefined it is not binded to any function or object this :

function memoize(fn) {
  return function(...args) {
     //in this example you can replace this by null since it is undefined 
      fn.apply(this, args);
  }
}

is similar to this :

function memoize(fn) {
  return function (args) {
      fn(args);
  }
}

Nested functions have access to the variables declared in the outer function, just like functions have access to the variables declared outside of the functions. you can check this link

in your example you are using the concept of closure and closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.

Belhadjer Samir
  • 1,461
  • 7
  • 15
  • Thank you for your answer ... but `return function(...args)` is way different than `return function function(args)` .. the first represent a function with **unknown num of arguments** ... the second is a signature for a function that shoudl have **only one arg** – Hend El-Sahli Mar 10 '21 at 18:36
  • Function (...args) in this cas i agree with u this represent function with unknown aargs in the case of declaration of the function but with you call a function and you pass it ...args that means you call a function with list of the args array , it's not the same – Belhadjer Samir Mar 10 '21 at 18:57
  • I Hope this help – Belhadjer Samir Mar 10 '21 at 18:58