Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 calling function with template literal but no parentheses [duplicate]

According to MDN, Tagged template literals can be used as follows:

var a = 5;
var b = 10;
function tag(strings, ...values) {
  alert(strings[0]); // "Hello "
  alert(strings[1]); // " world "
  alert(values[0]); // 15
  alert(values[1]); // 50
  return "Bazinga!";
}
tag `Hello ${ a + b } world ${ a * b }`; // "Bazinga!"

In the example above, the function tag is called without using parentheses.

I expected it should be called like tag(`Hello`), but that passes the string resulting from the template literal as the argument for the strings parameter of the function.

What's this special feature of calling functions without parentheses but with parameter?

like image 332
aWebDeveloper Avatar asked Mar 23 '16 00:03

aWebDeveloper


People also ask

What happens when you call a function without parentheses?

Return Functions – From the above discussion you can understand that, when the function is called with parentheses, the code is executed and returns the result. And, when it is called without parentheses, a function reference is returned to the callable.

What is the difference between calling function with parentheses and without in JavaScript?

With parenthesis the method is invoked because of the parenthesis, the result of that invocation will be stored in before_add. Without the parenthesis you store a reference (or "pointer" if you will) to the function in the variable.

What is this ${} in JavaScript?

A placeholder is represented by ${} , with anything within the curly brackets treated as JavaScript and anything outside the brackets treated as a string: const method = 'interpolation' const dynamicString = `This string is using ${method}.

Are template literals ES6?

Template Literals is an ES6 feature (JavaScript 2015).


1 Answers

What's this special feature of calling functions without parentheses?

This syntax for tagged templates is simply allowed by the the grammar:

MemberExpression : MemberExpression TemplateLiteral
CallExpression : CallExpression TemplateLiteral

These rules means that a MemberExpression or CallExpression followed by a TemplateLiteral is considered to be a function call. Additional note from the spec:

A tagged template is a function call where the arguments of the call are derived from a TemplateLiteral (12.2.9). The actual arguments include a template object (12.2.9.3) and the values produced by evaluating the expressions embedded within the TemplateLiteral.

If you are asking for why it was done this way, I cannot give you an answer.

However, if you think about it, the couldn't have just used "ordinary" function call syntax. tag(`...`) means that tag is passed a single argument, the result of evaluating the template literal. But as you can see in the example from MDN, tagged template functions actually get passed multiple arguments. It would certainly be more surprising if functions were called differently (internally) if they were passed a template literal vs if they were called with a different value. And then, how would call a function if you really wanted to pass a template literal to it?

Hence introducing a new syntax seems to make sense.


FWIW, this is the grammar for "ordinary" function calls:

CallExpression : MemberExpression Arguments
CallExpression : CallExpression Arguments
like image 161
Felix Kling Avatar answered Nov 17 '22 05:11

Felix Kling