
正文
ES6学习:两个面试题目--关于模板字符串
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
号称看完就能“让开发飞起来”,不过文中的两个面试题目的知识点并没包括在文中。
https://www.jianshu.com/p/287e0bb867ae
文中并没有完整的知识点去完成上面的两道题,这里给大家提示。第一题可用以下的例子处理
// 写法一
let str = 'return ' + '`Hello ${name}!`';
let func = new Function('name', str);
func('Jack') // "Hello Jack!" // 写法二
let str = '(name) => `Hello ${name}!`';
let func = eval.call(null, str);
func('Jack') // "Hello Jack!"
第二题可参照以下例子解决
let total = 30;
let msg = passthru`The total is ${total} (${total*1.05} with tax)`; function passthru(literals) {
let result = '';
let i = 0; while (i < literals.length) {
result += literals[i++];
if (i < arguments.length) {
result += arguments[i];
}
} return result;
} msg // "The total is 30 (31.5 with tax)"
来源于:http://es6.ruanyifeng.com/#docs/string#%E6%A8%A1%E6%9D%BF%E5%AD%97%E7%AC%A6%E4%B8%B2







