You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: manuscript/02.ECMAScript2015.md
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -100,7 +100,7 @@ If you take a look at the ES5 that this gets compiled down to, it makes a whole
100
100
101
101
Looking at this compiled code with such a simple example also makes this syntax seems a little silly, but it gets a whole lot more interesting when you consider a more real-world example. For instance, let's assume we want to use this chunk of HTML as a template to render the details of a Todo:
102
102
103
-
<div todo='[[Todo ID]]' class="list-group-item}">
103
+
<div todo='[[Todo ID]]' class="list-group-item">
104
104
<i class="[[ If Todo is complete, then "hidden" ]] text-success glyphicon glyphicon-ok"></i>
105
105
<span class="name">[[Name]]</span>
106
106
</div>
@@ -112,7 +112,7 @@ So, let's avoid all of that by converting this into a string template instead.
112
112
The first thing to do is wrap the HTML in backticks.
113
113
114
114
container.innerHTML = `
115
-
<div todo='[[Todo ID]]' class="list-group-item}">
115
+
<div todo='[[Todo ID]]' class="list-group-item">
116
116
<i class="[[ If Todo is complete, then "hidden" ]] text-success glyphicon glyphicon-ok"></i>
117
117
<span class="name">[[Name]]</span>
118
118
</div>
@@ -132,7 +132,7 @@ Then, rather than doing those search-and-replace operations, we can insert the v
132
132
}
133
133
134
134
container.innerHTML = `
135
-
<div todo='${todo.id}' class="list-group-item}">
135
+
<div todo='${todo.id}' class="list-group-item">
136
136
<i class="[[ If Todo is complete, then "hidden" ]] text-success glyphicon glyphicon-ok"></i>
137
137
<span class="name">${todo.name}</span>
138
138
</div>
@@ -143,7 +143,7 @@ One of the cool things about string templates is that you don't have to restrict
143
143
For example, I can figure out whether or not to render the "hidden" class on the icon element dynamically by introducing a conditional statement right there in the expression, like this:
@@ -153,7 +153,7 @@ When the value of todo.completed is true, the expression will evaluate to an emp
153
153
154
154
{title="app.js"}
155
155
~~~
156
-
container.innerHTML = "\n\t <div todo='" + todo.id + "' class=\"list-group-item}\">\n\t <i class=\"[[ If Todo is complete, then \"hidden\" ]] text-success glyphicon glyphicon-ok\"></i>\n\t <span class=\"name\">" + todo.name + "</span>\n\t </div>\n\t";
156
+
container.innerHTML = "\n\t <div todo='" + todo.id + "' class=\"list-group-item\">\n\t <i class=\"[[ If Todo is complete, then \"hidden\" ]] text-success glyphicon glyphicon-ok\"></i>\n\t <span class=\"name\">" + todo.name + "</span>\n\t </div>\n\t";
157
157
~~~
158
158
159
159
Once again, inspecting the compiled JavaScript reveals that - even with inline conditional statements and everything - this syntax eventually just ends up as a series of concatenated strings and expressions.
0 commit comments