美文网首页
ES6-字符串的解构赋值

ES6-字符串的解构赋值

作者: 东邪_黄药师 | 来源:发表于2018-10-31 17:16 被阅读0次

字符串也可以解构赋值
字符串被转换成了一个类似数组的对象。

字符串的解构赋值:

const [ a, b, c, d, e ] = "Hello";
console.log(a); //H
console.log(b); //e
console.log(c); //l
console.log(d); //l
console.log(e); //o

属性解构赋值
类似数组的对象都有一个length属性,因此还可以对这个属性解构赋值。
字符串的属性解构:

const { length: len } = "Hello";
console.log(len); //5

const { length } = "Hello World!";
console.log(length);//12

相关文章

网友评论

      本文标题:ES6-字符串的解构赋值

      本文链接:https://www.haomeiwen.com/subject/ingatqtx.html