Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can functions be called without parentheses when using template strings? [duplicate]

Tags:

I have a simple logging function:

function log(str) {   console.log('logged: ', str); } 

If I call it without parentheses (currently using Chrome's dev tools) and pass in a template string, like this:

log`foo` 

The output is: logged: ["foo", raw: Array[1]]

If I call it with parentheses,

log(`foo`) 

The output is: logged: foo

Why does calling a function using a template string an no parentheses work in Javascript? What is happening that causes the result to be different from calling it with parentheses?

like image 904
Sebastian Sandqvist Avatar asked Nov 11 '15 21:11

Sebastian Sandqvist


People also ask

What happens when you call a function without parentheses?

When we call a function with parentheses, the function gets execute and returns the result to the callable. In another case, when we call a function without parentheses, a function reference is sent to the callable rather than executing the function itself.

Why do some methods not have parentheses?

Attributes (e.g. imag) are like variables inside the object so you don't use parentheses to access them. Methods (e.g. islower()) are like functions inside the object so they do require parentheses to accept zero or more parameters and perform some work.

Do template strings support multiline strings?

Template Strings significantly simplify multiline strings. Simply include newlines where they are needed and BOOM. Here's an example: Any whitespace inside of the backtick syntax will also be considered part of the string.

What is a template literal string?

Template literals are literals delimited with backtick (`) characters, allowing for multi-line strings, for string interpolation with embedded expressions, and for special constructs called tagged templates.


1 Answers

The first example (log`foo`) allows the language specification to determine the values passed to the log function (See 12.3.7). The second example (log(`foo`)) explicitly passes a single argument.

Template literals can contain strings, as well as expressions. Sometimes you may wish to have more control over the compilation of a string from its string-parts, and expression-parts. In this case, you may be looking for tagged templates.

var name = "Jonathan"; var mssg = foo `Hello, ${name}. Nice name, ${name}.`;  function foo ( strings, ...values ) {     console.log( strings ); //["Hello, ", ". Nice name, ", ".", raw: Array[3]]     console.log( values  ); //["Jonathan", "Jonathan"] } 

Notice here how all of the strings are passed in through the first argument. As well, all of the interpolated value expressions are passed in through the rest of the parameters (pulled together into an array here).

With this added control, we could do all sorts of things, such as localization. In this example, the language specification determines the appropriate values to pass to the function — the developer doesn't determine this.

To contrast, when you call log(foo), you wind up getting only the resulting string. No objects, no parts, no raw values.

like image 150
Sampson Avatar answered Sep 28 '22 03:09

Sampson