ES6中字符串使用小tips

ES6中,字符串添加了Iterable接口,但是不可以使用方括号对属性值进行修改

let str="abc";
console.log(str[1]);//b
//目的:修改字符串的第二个字符为大写B
str[1]='B';
console.log(str);//abc

解决:

let str=[..."abc"];
str[1]="B";
console.log(str.join(""));//aBc

When using bracket notation for character access, attempting to delete or assign a value to these properties will not succeed. The properties involved are neither writable nor configurable. (See Object.defineProperty() for more information.) --MDN

你可能感兴趣的:(ES6,es6)