const str = '你好,世界!';
const length = Array.from(str).reduce((acc, cur) => acc + (cur.charCodeAt(0) > 255 ? 2 : 1), 0);
console.log(length); // Output: 7

在JavaScript中 字符串使用length ,存在汉字会导致字符串的长度 不准。使用循环,通过charCodeAt()方法把单个字符转成Unicode字符来判断。