将ES6 Unicode文字和ES6模板文字结合在一起 [英] Combining ES6 unicode literals with ES6 template literals

查看:107
本文介绍了将ES6 Unicode文字和ES6模板文字结合在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想在ES6/ES2015 javascript中打印unicode汉字,我可以这样做:

If I want to print a unicode Chinese character in ES6/ES2015 javascript, I can do this:

console.log(`\u{4eb0}`);

同样,如果我想将变量插值到模板字符串文字中,则可以执行以下操作:

Likewise, if I want to interpolate a variable into a template string literal, I can do this:

let x = "48b0";
console.log(`The character code is ${ x.toUpperCase() }.`);

但是,我似乎无法将两者结合起来以打印出例如40个连续的unicode汉字的列表.这不起作用:

However, it seems that I can't combine the two to print a list of, for example, 40 consecutive unicode Chinese characters. This doesn't work:

for (let i = 0, firstCharCode = parseInt("4eb0", 16); i < 40; ++i) {
    let hexCharCode = (firstCharCode + i).toString(16);
    console.log(`\u{${ hexCharCode }}`); // generates SyntaxError
}

所以我问是否有可能.

推荐答案

您将需要使用

You would need to use String.fromCodePoint(), which accepts a number and returns a character.

您不能使用文字,因为...好吧...它不再是文字了.文字不能作为过程的结果而产生,需要将它们写为文字.

You can't do it with literals because... well... it's not a literal anymore. Literals can't be produced as the result of a procedure, they need to be written as literals.

这篇关于将ES6 Unicode文字和ES6模板文字结合在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆