es2015(es6)基础知识整理(更新中...)

时间:2023-12-22 14:21:20

1.let

let可以声明块级作用域变量

 'use strict';

 if (true) {
let app = 'apple';
} console.log(app);  //外面是访问不到app的

2.const

const可以声明常量

 'use strict';

 const app = 'apple';
console.log(app); const app = 'peach';
console.log(app);  //报错,常量不能多次赋值

3.Destructuring 解构

解构赋值允许使用类似数组或对象字面量的语法将数组和对象的属性赋给各种变量。

'use strict';

function breakfast () {
return ['egg','bread','milk'];
} //传统方法
var arr = breakfast(), egg = arr[0], bread = arr[1], milk = arr[2];
console.log(egg,bread,milk); //es6
let [t1,t2,t3] = breakfast();
console.log(t1,t2,t3);

4.对象解构

'use strict';

function breakfast () {
return {egg:'egg', milk:'milk', bread:'bread'};
} let {egg: a1, milk: a2, bread: a3} = breakfast(); //a1,a2,a3是自己定义的名字
console.log(a1,a2,a3);

5.字符模板

'use strict';

let food1 = 'egg', food2 = 'bread';

let str = `今天的早餐是 ${food1} 和 
      ${food2}`;  //通过反斜线引号可以对字符串换行 console.log(str);

6.字符串相关函数

'use strict';

let food1 = 'egg', food2 = 'bread';

let str = `今天的早餐是 ${food1} 和
${food2}`;  console.log(str.startsWith('今天'),str.endsWith('bread'),str.includes('早餐'));  //true true true

7.函数默认值

 'use strict';

 function breadfast (food = '食物', drink = '饮料') {
console.log(`${food} ${drink}`);
} breadfast(); //输出默认值
breadfast('面包', '啤酒'); //输出给定值

8.  ...操作符

 'use strict';

 function breakfast (food, drink, ...others) {        //...表示后面除了前面两个参数,其余参数都放在others这个数组里
console.log(food, drink, others);
console.log(food, drink, ...others); //将others数组里面的值展开
} breakfast('面包', '牛奶', '培根', '香肠');

9.Arrow Function(箭头函数)

 'use strict';

 //breakfast是函数名字,箭头前面是传入的参数,箭头后面是返回值
let breakfast = (food,drink) => food + drink; console.log(breakfast('面包', '牛奶')); //箭头后面的大括号可以写函数的具体操作
let lunch = (food, drink) => {
return '今天午餐吃' + food + ',喝' +drink;
}; console.log(lunch('鸡肉', '汤'));

10.定义对象

 'use strict';

 let food = '鸡肉', drink = '汤';

 let breakfast = {
food: food, //添加属性
drink: drink,
eat(){} //添加方法
}; console.log(breakfast); //添加属性的其他方法
let food1 = 'food1'; breakfast.afood = '面条';
breakfast['hot drink'] = '牛奶';
breakfast[food1] = '蔬菜'; //这里的food1是变量food1的值 console.log(breakfast);

11.setPrototypeOf(改变原型)

 'use strict';

 let lunch = {
getDrink(){
return '啤酒';
}
}; let dinner = {
getDrink(){
return '可乐';
}
}; let food1 = Object.create(lunch);
console.log(food1.getDrink()); //啤酒
console.log(Object.getPrototypeOf(food1) === lunch); //true Object.setPrototypeOf(food1, dinner);
console.log(food1.getDrink()); //可乐
console.log(Object.getPrototypeOf(food1) === dinner); //true

12.__proto__

 'use strict';

 let lunch = {
getDrink(){
return '啤酒';
}
}; let dinner = {
getDrink(){
return '可乐';
}
}; //__proto__
let food2 = {
__proto__: lunch //通过__proto__来指定food2的prototype
}; console.log(food2.getDrink());
console.log(Object.getPrototypeOf(food2) === lunch); //true food2.__proto__ = dinner;
console.log(food2.getDrink());
console.log(Object.getPrototypeOf(food2) === dinner); //true

13.super

 'use strict';

 let lunch = {
getDrink(){
return '啤酒';
}
}; let dinner = {
getDrink(){
return '可乐';
}
}; //super
let food3 = {
__proto__: lunch,
getDrink(){
return super.getDrink() + '和汤'; //super.getDrink()是调用原型的getDrink方法
}
}; console.log(food3.getDrink());

14.iterator迭代器

每次迭代后都会返回一个对象{value:xx,done:true/false},value表示返回的值,done表示当前还有没有东西可以迭代,没有返回true,否则返回false

 'use strict';

 function chef(foods){
let i = 0; return {
next(){
let done = (i >= foods.length);
let value = !done ? foods[i++] : undefined; return {
value: value,
done: done
}
}
}
} let meal = chef(['黄瓜', '鸡蛋']);
console.log(meal.next()); //Object {value: "黄瓜", done: false}
console.log(meal.next()); //Object {value: "鸡蛋", done: false}
console.log(meal.next()); //Object {value: undefined, done: true}

15.es6中的class

 'use strict';

 class Chef {
constructor(food){ //创建一个Chef类实例时会自动执行constructor方法
this.food = food;
} cook(){
console.log(this.food);
}
} let chef1 = new Chef('鸡蛋');
chef1.cook();

16.Set

 'use strict';

 //Set类似于数组
let food = new Set('鸡蛋');
console.log(food); food.add('菜'); //添加新的项
food.add('豆'); console.log(food); console.log(food.size); //food的长度
console.log(food.has('菜')); //判断是否含有某一项
food.delete('豆'); //删除某一项
console.log(food); food.forEach(food => { //批量对food中的项进行操作
console.log(food);
}); food.clear(); //清空food
console.log(food);