首页 > 试题广场 >

关于ES6解构表达式let[a,b,c,d,e]="hell

[单选题]
关于ES6解构表达式
let [a,b, c,d, e] = "hello"; 
描述正确的是()
  • e = "hello";其它都为undefined
  • 当中 a = "h", b = "e";
  • 语法报错
这个属于字符串的变量解析,具体可以看阮一峰老师的ES6教程,字符串会被转化成一个类数组的对象,因此每一个字符都会和左边一一对应
发表于 2020-09-25 19:03:31 回复(0)
阮一峰老师的ECMAScript 6入门:https://es6.ruanyifeng.com/#docs/destructuring
字符串的解构赋值
字符串也可以解构赋值,因为此时字符串被转换成一个类似数组的对象
let [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 属性,因此还可以对这个属性解构赋值
let {length : len} = 'hello';
console.log(len);    //5
发表于 2020-11-30 16:56:18 回复(0)

字符串也可以解构赋值。这是因为此时,字符串被转换成了一个类似数组的对象。

const [a, b, c, d, e] = 'hello'; a // "h" b // "e" c // "l" d // "l" e // "o" 

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

let {length : len} = 'hello'; len // 5
发表于 2020-10-04 16:01:22 回复(1)
<p>为什么可以这么写啊</p>
发表于 2020-09-24 23:57:12 回复(0)