1. let & const
定义变量使用let
for(let i=0; i<5; i++) {
console.log(i);
}
console.log("外"+i);
定义常量使用const
const j=0;
j=2;
2. 字符串的操作
- includes、startwith、endwith
- 字符串模板
3. 解构表达式
- 解构数组
.
- 解构对象
4.函数优化
-
函数参数默认值
function fun1(a,b = 1) { // if(!b) { // b = 1; // } // b = b || 1; console.log(a/b); } fun1(10);
-
箭头函数
let fun2 =a => { a = 1000; console.log(a); } fun2(10); let fun3 = (a,b=2) => { console.log(a/b); } fun2(10)
-
对象内方法
let person = { name: "そら", eat: function(food) { console.log(this.name+"吃了"+food); }, eat1: food => console.log(person.name+"吃了"+food), eat2(food) {//常用 console.log(this.name+"吃了"+food); } } person.eat("香蕉"); person.eat("苹果"); person.eat("梨");
-
解构形参
let p = { name: "小芳", age: 1 } // let fun3 = (p1) => console.log(p1.name); let fun3 = ({name}) => console.log(name); fun3(p);
4.map和reduce
-
map
let arr = [10,20,30]; > undefined arr.map(a => a * 10) > (3) [100, 200, 300] let persons=[{name: "zhangsan",age: 1}, {name: "lisi",age:2}]; > undefined persons.map(p => p.name) > (2) ["zhangsan", "lisi"]
-
reduce
let arr = [10,20,30]; arr.reduce((a,b) => a+b) > 60 arr.reduce((a,b) => a+b, 100) > 160
5. 对象扩展
6.数组扩展
- find(callback)、findIndex(callback)、includes(数组元素)
let arr = [10,20,30,40,50]
> undefined
arr.find(a => a==40)
> 40
let persons=[{name: "zhangsan",age: 1}, {name: "lisi",age:2}];
> undefined
persons.find(p => p.age == 2)
> {name: "lisi", age: 2}
persons.findIndex(p => p.age==2)
> 1
arr.includes(60)
> false