Ben Aston
Ben Aston

Reputation: 55779

Template strings in ES2015

What is the meaning of "passed array is frozen" in the context of template strings in ES2015?

Source: http://node.green

Upvotes: 3

Views: 221

Answers (3)

B.V.S Bharat Kumar
B.V.S Bharat Kumar

Reputation: 222

Templates strings are the ECMA6 concepts where we can write and append multiple line code with " ` " as before we use to do with double quotes with string concat.

var container = document.getElementById('container');

var todo = {
    id: 123,
    name: "Pick up dry cleaning",
    completed: true
}

container.innerHTML = `
<div todo='$(todo.id)' class="list-group-item"> 
 <i class='${todo.completed}?"hidden":"glyphicon-ok"} text-success glyphicon'></i>
 <span class="name">${todo.name}</span>
`
Templates strings are the ECMA6 concepts where we can write and append multiple line code with " ` " as before we use to do with double quotes with string concat. 
  <div id="container" class="container">
    </div>

Upvotes: 0

Bergi
Bergi

Reputation: 665555

It doesn't mean any special in the context of template strings. It just means that the array is frozen. From the docs:

An object is frozen if and only if it is not extensible, all its properties are non-configurable, and all its data properties (that is, properties which are not accessor properties with getter or setter components) are non-writable.

So the array of strings that is getting passed into the tag function is essentially immutable. This is supposed to allow engines to store it as a constant and repeatedly pass the same array object every time the tagged template literal is evaluated.

Upvotes: 3

thefourtheye
thefourtheye

Reputation: 239683

You can define custom template literal tags in ECMA Script 6. For example,

(function(parts, a, b) {
  return Object.isFrozen(parts) && Object.isFrozen(parts.raw);
}) `foo${0}bar${0}baz`;

Source: https://kangax.github.io/compat-table/es6/#test-template_literals

In the above code, the function object is the "tag" for the template literal.

When the template literal is evaluated, the tag function is called with all the parts of the template literal (in this case, they are foo, bar, and baz) as an array. The test case you are seeing is to make sure that array object is already frozen or not.

The test basically checks this section of the ES6 Spec,

Perform SetIntegrityLevel(rawObj, "frozen").
...
Perform SetIntegrityLevel(template, "frozen").

Upvotes: 3

Related Questions