【摘】开发者需要知道的Unicode

摘译自Dmitri Pavlutin的文章What every JavaScript developer should know about Unicode

  • javascript 对待 string 为编码序列而不是字符
  • 大部分 string 方法不是 Unicode-aware,包括 indexOf,slice
  • […str] 或者 Array.from(str) 可以正确确认 length
const smile = "😀";

smile.length; // => 2
const smile = "\uD83D\uDE00";

smile === "😀"; // => true
smile.length; // => 2
const message = "Hello!";
const smile = "😀";

[...message].length; // => 6
[...smile].length; // => 1

Total views.

© 2013 - 2024. All rights reserved.

Powered by Hydejack v6.6.1