Skip to content

Commit c54a383

Browse files
committed
docs(string): edit 模板字符串
1 parent 67eee04 commit c54a383

File tree

1 file changed

+4
-9
lines changed

1 file changed

+4
-9
lines changed

docs/string.md

+4-9
Original file line numberDiff line numberDiff line change
@@ -497,20 +497,15 @@ console.log(tmpl(data));
497497
// </table>
498498
```
499499

500-
如果需要引用模板字符串本身,在需要时执行,可以像下面这样写
500+
如果需要引用模板字符串本身,在需要时执行,可以写成函数
501501

502502
```javascript
503-
// 写法一
504-
let str = 'return ' + '`Hello ${name}!`';
505-
let func = new Function('name', str);
506-
func('Jack') // "Hello Jack!"
507-
508-
// 写法二
509-
let str = '(name) => `Hello ${name}!`';
510-
let func = eval.call(null, str);
503+
let func = (name) => `Hello ${name}!`;
511504
func('Jack') // "Hello Jack!"
512505
```
513506

507+
上面代码中,模板字符串写成了一个函数的返回值。执行这个函数,就相当于执行这个模板字符串了。
508+
514509
## 实例:模板编译
515510

516511
下面,我们来看一个通过模板字符串,生成正式模板的实例。

0 commit comments

Comments
 (0)