0

I was reading the book :

JavaScript The Definitive Guide

and got an issue in understanding an example code given in 8.3.4 as The Spread Operator for Function Calls.

Here's the code :

// This function takes a function and returns a wrapped version
function timed(f) {
 return function(...args) { // Collect args into a rest parameter array
 console.log(`Entering function ${f.name}`);
 let startTime = Date.now();
 try {
 // Pass all of our arguments to the wrapped function
 return f(...args); // Spread the args back out again
 }
 finally {
 // Before we return the wrapped return value, print elapsed time.
 console.log(`Exiting ${f.name} after ${Date.now()-startTime}ms`);
 }
};
}
// Compute the sum of the numbers between 1 and n by brute force
function benchmark(n) {
 let sum = 0;
 for(let i = 1; i <= n; i++) sum += i;
 return sum;
}

When Invoked gives this

// Now invoke the timed version of that test function
timed(benchmark)(1000000) 
Entering function benchmark
Exiting benchmark after 11ms
500000500000

My Problem

I am not able to understand what these 2 Js Statements are for and their working?

// 1st
return function(...args)

//2nd
return f(...args); 

I haven't seen any function like this without a name. Please help me in understanding.

Thanks

Baazigar
  • 47
  • 1
  • 7
  • 1
    Does this answer your question? [What is the meaning of "...args" (three dots) in a function definition?](https://stackoverflow.com/questions/42184674/what-is-the-meaning-of-args-three-dots-in-a-function-definition) – aaandri98 Jul 27 '21 at 10:15
  • Hi @aaandri98 No, My question is different. I know the use of spread operator. I can't understand this : `return function(...args)`. What is `function` here. How can it be even that and not name of a function? – Baazigar Jul 27 '21 at 10:19
  • The function is named `timed` and it's using `function` here. – Baazigar Jul 27 '21 at 10:20
  • @Baazigar it's not the "spread operator". This is rest syntax for function parameters. – VLAZ Jul 27 '21 at 10:21
  • Sorry for my typo. I meant rest only. – Baazigar Jul 27 '21 at 10:23
  • I know that rest parameter takes extra remaining arguments. But which function is the code calling here after `return`? – Baazigar Jul 27 '21 at 10:25
  • The statement `return f(...args);` can be understood as `f` is a function name. – Baazigar Jul 27 '21 at 10:26
  • `return function(...args) {}` will just return a new function. That function then needs to be executed. [Functions that return a function](https://stackoverflow.com/q/7629891) – VLAZ Jul 27 '21 at 10:26
  • @VLAZ But how is it returning without a function name? Don't we get error `SyntaxError: Function statements require a function name`. I have learnt this. – Baazigar Jul 27 '21 at 10:31
  • Please provide a guide or webpage where can I read about this statement – Baazigar Jul 27 '21 at 10:32
  • function *expressions* do not require a name. Only function *statements*/declarations do. `x = function() {}` is a function expression and it's equally valid as `x = function foo() {}` [var functionName = function() {} vs function functionName() {}](https://stackoverflow.com/q/336859) – VLAZ Jul 27 '21 at 10:32
  • I got it now somewhat. I was confused as there is no x nor any equal to sign. – Baazigar Jul 27 '21 at 10:37
  • I feel as if it's JavaScript Functions that return a function which I haven't read till now. – Baazigar Jul 27 '21 at 10:40

0 Answers0