LVPHP

ES6-info

使用拓展运算符 … 复制数组

1
2
3
4
5
6
"use strict"
const items = [1,2,3,4]
const itemscopy = [...items]
console.log(itemscopy); //note out: [ 1, 2, 3, 4 ]
itemscopy[0] = 6;
console.log(items, itemscopy); //note out: [ 1, 2, 3, 4 ] [ 6, 2, 3, 4 ]

使用解构存取和使用多属性对象

1
2
3
4
5
6
7
8
"use strict"
function getFullName(obj) {
const { firstName, lastName } = obj;
return `${firstName} ${lastName}`;
}
let str = getFullName({firstName:"wangbo", lastName:"bo", age:30});
console.log(str); //note out: wangbo bo

对数组使用解构赋值

1
2
3
4
"use strict"
const arr = [1, 2, 3, 4];
const [first, second] = arr;
console.log(first, second); //note out: 1 2

需要回传多个值时,使用对象解构,而不是数组解构

1
2
3
4
5
6
"use strict"
function processInput() {
return {left: 10, right: 20, top: 30, bottom: 40};
}
const { top, left } = processInput();
console.log(top, left); //note out: 30 10

程序化生成字符串时,使用模板字符串代替字符串连接

1
2
3
4
5
6
"use strict"
function sayHi(name) {
return `How are you, ${name}?`;
}
const str = sayHi("wangbo");
console.log(str); //note out: How are you, wangbo?

函数的参数指定默认值

1
2
3
4
"use strict"
function handleThings(opts = {}) {
}

使用箭头函数符号

1
2
3
4
5
6
"use strict"
const items = [1,2,3,4];
let tmp = items.map((x)=>{return x*x;});
console.log(tmp); //note out: [ 1, 4, 9, 16 ]
let arr = items.map(x => x * x);
console.log(arr); //note out: [ 1, 4, 9, 16 ]