edit module, array

This commit is contained in:
ruanyf
2015-08-31 11:12:16 +08:00
parent 6b1ee303ff
commit 9dcdc6a9e8
5 changed files with 124 additions and 31 deletions
+11 -4
View File
@@ -286,14 +286,14 @@ function authorize(user, action) {
var x = 1;
var y = 2;
console.log(`${x} + ${y} = ${x+y}`)
`${x} + ${y} = ${x + y}`
// "1 + 2 = 3"
console.log(`${x} + ${y*2} = ${x+y*2}`)
`${x} + ${y * 2} = ${x + y * 2}`
// "1 + 4 = 5"
var obj = {x: 1, y: 2};
console.log(`${obj.x + obj.y}`)
`${obj.x + obj.y}`
// 3
```
@@ -304,7 +304,7 @@ function fn() {
return "Hello World";
}
console.log(`foo ${fn()} bar`);
`foo ${fn()} bar`
// foo Hello World bar
```
@@ -318,6 +318,13 @@ var msg = `Hello, ${place}`;
// 报错
```
由于模板字符串的大括号内部,就是执行JavaScript代码,因此如果表达式放在引号之中,将会原样输出。
```javascript
`Hello ${'World'}`
// "Hello World"
```
## 标签模板
模板字符串的功能,不仅仅是上面这些。它可以紧跟在一个函数名后面,该函数将被调用来处理这个模板字符串。这被称为“标签模板”功能(tagged template)。