-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlesson_4.js
More file actions
42 lines (31 loc) · 987 Bytes
/
lesson_4.js
File metadata and controls
42 lines (31 loc) · 987 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//JS NUggets: Template Literals
// multi-line strings
console.log(`string text line1
string text line2`)
// expression interpolation
var a = 5;
var b = 10;
console.log(`fifteen is ${a + b} and\nnot ${2 * a + b}.`)
// Tagged template literals
function tag(strings, ...value) {
console.log(strings);
console.log(value);
return "JS Nuggets";
}
tag `Hello ${a + b} world ${a * b}`;
console.log(tag `Hello ${a + b} world ${a * b}`);
function template(strings, ...keys) {
return (function(...values) {
var dict = values[values.length - 1] || {};
var result = [strings[0]];
keys.forEach(function(key, i){
var value = Number.isInteger(key) ? values[key] : dict[key];
result.push(value, strings[i + 1]);
});
return result.join('');
});
}
var t1Closure = template`${0}${1}${0}!`;
console.log(t1Closure('Y', 'A'));
var t2Closure = template`${0} ${'foo'}`;
console.log(t2Closure('Hello', {foo: 'World'}))