3

For a function like this. There are two ways to invoke the foo funciton.

function foo(){
   console.log(arguments);
}
foo(123);  // a normal way to invoke a function.
foo`123`;  // also a way to invoke a function.

I want to know the differences about the two way to invoke a function in details. And the usages of the second way to invoke a function.

quan lili
  • 109
  • 6
  • 2
    The second is called a tagged template literal, or a tagged function call. The difference is what arguments the function receives: In your first example, it receives a single argument that's a number (`123`). In your second example it receives a single argument that's an array containing a string (`["123"]`). The array also has an extra property on it called `raw`. The purpose of tagged template literals is that they let you build useful things from the component parts of the template. If you had substitutions in the template (```foo`123 ${x}`;```), `foo` would receive additional ... – T.J. Crowder Jun 13 '20 at 08:33
  • 1
    ...arguments with those substitution values. – T.J. Crowder Jun 13 '20 at 08:33
  • (FWIW, I go into tag functions and template literals in Chapter 10 of my new book. See my profile if you're interested.) – T.J. Crowder Jun 13 '20 at 08:34
  • *"There are two ways to invoke the foo funciton "* [Invoking a function without parentheses](https://stackoverflow.com/a/35949617) – adiga Jun 13 '20 at 08:37

0 Answers0