1.块作用域 let
定义的变量只在本作用域内有效
1 2 3 4 5 6 7 8 9
| 'use strict';
if(true){ var fruit = 'apple'; let fruit = 'apple'; }
console.log(fruit);
|
2.恒量 const
无法重新定义值,是限制分配值的动作,不是限制值
1 2 3 4 5 6 7 8 9 10 11
| 'use strict';
const fruit = 'apple'; console.log(fruit);
const fruit = 'orange'; console.log(fruit);
const fruits = []; fruits.push('🍎'); console.log(fruits);
|
3.解构对象
函数返回的是个对象,用解构方法可用直接进行赋值,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| 'use strict';
function breakfast(){ return {fruit:'fruit', drink:'drink', desert:'dessert'}; }
var tem = breakfast(); desert = tem[2], drink=tmp[1], fruit=tmp[0];
let [desert, drink, fruit] = breakfast();
console.log(desert, drink, fruit);
|
4. 模板字符串
在 ${variable} 中变量可以是表达式,也可以直接回车换行多行显示
1 2 3 4 5 6 7 8 9 10 11 12
| 'use strict';
let desert = 'cake', drink = 'orange';
let breakfast = "Today' breakfast is " + desert + "&" + drink;
let breakfast = `Today's breakfast is ${desert} and ${drink}`;
console.log(breakfast);
|
5. Tagged Template - 带标签模板
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| 'use strict';
let desert = 'cake', drink = 'orange'; let breakfast = kitchen`Today's breakfast is ${desert} and ${drink}`;
fuction kitchen(strings, ...values){ console.log(strings); console.log(values);
let result = ''; for(let i=0; i<values; i++){ result += string[i]; result += values[i]; } result += strings[strings.length-1];
return result; }
console.log(breakfast);
|
6. 判断是否含有其他字符串
1 2 3 4 5 6 7 8 9 10 11 12 13
| use 'strict';
let breakfast = 'Today is fine';
console.log(breakfast.startsWith('today'));
console.log(breakfast.endsWith('today'));
console.log(breakfast.includes('fine'));
|
7. default parameter values - 默认参数
1 2 3 4 5 6 7
| use 'strict';
function breakfast(desert = 'orange', drink = 'water'){ return `${desert} & ${drink}`; }
console.log(breakfast());
|
8.Spread - 展开操作符
1 2 3 4 5 6 7 8 9
| use 'strict';
let fruits = ['apple', 'banana'], food = ['cake', ...fruits];
console.log(fruits); console.log(...fruits); console.log(food);
|
9. Rest - 剩余操作符
1 2 3 4 5 6 7 8 9 10
| use 'strict';
function breakfast(desert, drink, ...foods){ console.log(desert, drink, foods); console.log(desert, drink, ...foods); }
breakfast("a", "b", "c", "d"); breakfast("a", "b", "c", "d");
|
10. name属性 - 函数都名字
1 2 3 4 5 6 7 8 9 10 11 12 13
| use 'strict';
let funName = function (argument){ }
console.log(funName.name);
let B = function myName(argument){ } console.log(B.name);
|
11.Arrow Functions - 箭头函数
1 2 3 4 5 6 7 8 9 10
|
let breakfast = (desert, drink)=> { return desert + drink; };
var breakfast = function breakfast(desert, drink){ return desert + drink; }
|
12. 对象表达式
1 2 3 4 5 6 7 8 9 10 11 12
| 'use strict';
let desert = '🍰', fruit = '🍎';
let food = {
desert, fruit, breakfast() {} };
console.log(food);
|
13. 对象属性名
1 2 3 4 5 6 7 8 9 10
| 'use strict';
let food = {}; let drink = 'hot drink';
food.desert = '🍰'; food.drink = '🍶'; food[drink] = '🍶';
console.log(food);
|
14. Object.is() - 判断2个值是否相等
1 2 3 4
| 'use strict';
console.log(NaN == NaN); console.log(Object.is(NaN, NaN));
|
15.Object.assign() - 把一个对象的值复制到另外一个对象
1 2 3 4 5 6 7 8 9 10
| 'use strict';
let breakfast = {};
Object.assign( breakfast, {drink:'🍺'} );
console.log(breakfast);
|
16. Object.setPrototypeOf() - 设置对象的prototype
表达式为: Object.setPrototypeOf(obj, targetObj)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| 'use strict';
let breakfast = { getDrink(){ return 'apple'; }; }
let dinner = { getDrink(){ return 'beer'; }; }
let sunday = Object.create(breakfast ); console.log(sunday.getDrink());
console.log(Object.getPrototypeOf(sunday, breakfast));
Object.setPrototypeOf(sunday, dinner); console.log(sunday.getDrink()); console.log(Object.getPrototypeOf(sunday, breakfast));
|
17. proto
得到或者设置对象的prototype
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| 'use strict'
let breakfast = { getDrink(){ return 'water'; } };
let dinner = { getDrink(){ return 'beer'; } };
let sunday = { __proto__: breakfast };
console.log(sunday.getDrink());
sunday.__proto__ = dinner; console.log(sunday.getDrink());
console.log(Object.getPrototypeOf(sunday) === dinner);
|
18. super
可重定义覆盖原proto的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| 'use strict';
let breakfast = { getDrink(){ return 'water'; } };
let dinner = { getDrink(){ return 'beer'; } };
let sunday = { __proto__: breakfast, getDrink(){ return super.getDrink() + 'cake'; } };
console.log(sunday.getDrink());
|
19. Itrators - 迭代器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| 'use strict';
function chef(foods) { let i = 0; let done = (i>=foods.length); let value = !done ? foods[i++] : undifined;
return { next(){ return { value: value, done: done }; } }; }
json = chef(['apple', 'banana']); console.log(json.next()); console.log(json.next()); console.log(json.next());
|
20. Generators - 生成迭代器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| 'use strict';
let chef = function* (foods){ for(var i = 0; i < foods.length; i++){ yeild foods[i]; } }
let json = chef(['🍎', '🍌']);
console.log(json.next()); console.log(json.next()); console.log(json.next());
|
21. classes - 类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| 'use strict';
class Chef{ constructor(food){ this.food=food; this.dish = []; }
get menu() = { return this.dish; }
cook(){ console.log(this.food); }
set menu(dish){ this.dish.push(this.dish); } }
let json = new Chef('🍌');
json.cook();
|
22. get 和 set
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| 'use strict';
class Chef{ constructor(food){ this.food=food; this.dish = []; }
get menu() = { return this.dish; }
set menu(dish){ this.dish.push(this.dish); }
cook(){ console.log(this.food); }
}
let json = new Chef();
console.log(json.menu = '🍜'); console.log(json.menu = '🍕'); console.log(json.menu);
|
23. static - 静态方法
无需实例化类的方法
1 2 3 4 5 6 7 8 9 10
| 'use strict';
class Chef{ static cook (food){ console.log(food); } }
Chef.cook('🍅');
|
24. extends - 继承
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| 'use strict';
class Person { constructor(name, birth){ return this.name = name; return this.birth = birth; } intro(){ return `${name}.'s year is ${birth}`; } }
class Chef extens Person{ constructor(name, birth){ super(name, birth); } }
let Json = new Chef("Jsonlocker", "2019-02-01"); console.log(Json.intro);
|
25. Set - 集合
不包含重复项目的集合
1 2 3 4 5 6 7 8 9 10 11 12
| 'use strict';
let foods = new Set('🌲🍉🍓'); console.log(foods); foods.add('🍎') console.log(foods.size); console.log(foods.has('🍎')); foods.delete('🍎'); foods.forEach(food => { console.log(food); }); foods.clear();
|
26. Map
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| 'use strict';
let foods = new Map(); let fruit = {}, cook = function(){} , desert = 'banana'; foods.set(fruit, '🍋'); foods.set(cook, '🔪'); foods.set(desert, '🥧');
console.log(foods.size); console.log(foods.get(fruit)); foods.delete(desert); console.log(foods.has(desert));
foods.forEach((value, key) =>{ console.log(`${key} => ${value}`); });
foods.clear();
|