ES6结构赋值学习笔记
let [a, b, c] = [1, 2, 3]
console.log(a, b, c)
let [, , d] = ['', '', 7]
console.log(d)
let [e, ...f] = [1, 2, 3, 4, 5]
console.log(e, f)
let [g, h, ...i] = [1]
console.log(g, h, i)
function* fibs() {
let a = 0;
let b = 1;
while (true) {
yield a;
[a, b] = [b, a + b];
}
}
let [first, second, third, fourth, fifth, sixth] = fibs();
console.log(first, second, third, fourth, fifth, sixth)
let [j = true] = []
let [k, l = 1] = [2]
let {m, n} = {m: 1, n: 2}
const {log} = console
log(111)
let {log: los} = console
los(222)
const node = {
loc: {
start: {
line: 1,
column: 5
}
}
};
let {loc, loc: {start}, loc: {start: {line}}} = node;
console.log(line)
console.log(loc)
console.log(start)
{
let arr = [1, 2, 3];
let {0: first, [arr.length - 1]: last} = arr;
console.log(first)
console.log(last)
}
{
const [a, b, c, d, e] = 'hello';
console.log(a, b, c, d, e)
let {length: len} = 'hello';
console.log(len)
}
{
let arr = [1, undefined, 3]
arr = arr.map((x = 'yes') => x);
console.log(arr)
}
{
let x = 1;
let y = 2;
[x, y] = [y, x]
console.log(x, y)
}
{
function example() {
return [1, 2, 3]
}
let [a, b, c] = example()
console.log(a, b, c)
function examples() {
return {
foo: 1,
bar: 2
};
}
let {foo, bar} = examples();
console.log(foo, bar)
}
{
let jsonData = {
id: 42,
status: "OK",
data: [867, 5309]
};
let {id, status, data: number} = jsonData;
console.log(id, status, number);
}
{
const map = new Map()
map.set('first', 'hello')
map.set('second', 'world')
for (let [key, val] of map) {
console.log(key + ' is ' + val)
}
for (let [key] of map) {
}
for (let [, val] of map) {
}
}
{
const {SourceMapConsumer, SourceNode} = "";
}