课程地址: 【JS 老毕】Javascript ES6 基础+核心课程
9. 字符串方法和 for of
includes 是否包含,返回 true/false
1 2 3
| const string = "abc"; const substring = "ab"; console.log(string.includes(substring));
|
startsWith 是否是以某个字符串开始, 返回 true/false
endsWith 是否是以某个字符串结尾, 返回 true/false
1 2 3
| const string = "abc"; const substring = "ab"; console.log(string.startsWith(substring));
|
for of
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
| const tests = ["a", "b", "c", "d", "e"]; for (const test of tests) { console.log(test); }
let maps = new Map(); maps.set("name", "ahui"); maps.set("phone", "17600888888"); maps.set("city", ["北京", "上海"]);
for (const [key, value] of maps) { console.log(key, value); }
const tests = ["a", "b", "c", "d", "e"];
for (const test of tests.entries()) { console.log(test); }
|
8. 函数参数默认值
1 2 3 4 5
| function orderCombo(comboName = "鸡块", drink = "可乐") { console.log(comboName); console.log(drink); } orderCombo("蛋炒饭");
|
7. 剩余参数和扩展参数 …
剩余参数是把多个打包成一个数组
将一个数据拆分成多个项
1 2 3 4 5 6 7 8 9 10 11
| const tests = ["a", "b", "c", "d", "e"]; const [a, b, ...c] = tests;
console.log(a, b, c);
const a = "a"; const b = "b"; const c = ["a", "b", "c"]; console.log([a, b, ...c]);
|
6. 结构赋值
object 结构赋值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| const person = { name: "ahui", age: "18", city: "BJ", social: { www: "leejs.cn"; email: "ahuinet@163.com" } }
const {name, age, city} = person const {www, email} = person.social const {name, social: {www}} = person console.log(name, age, city, www, email)
const { name: ahuiName } = person
const { mony = 1008610010 } = person
|
数组的结构赋值
1 2 3 4 5 6 7 8 9
| const info = "ahui, 18, 1000000000"; const person = infor.split(","); const [name, age, mony] = person; console.log(name, age, mony);
let a = 1; let b = 2; [a, b] = [b, a]; console.log(a, b);
|
5. map 对象
map set 数据的时候,key 是唯一的,如果 set 一个原来有的属性,就会更新原来对应 key 的值
1 2 3 4 5 6 7 8 9 10 11 12
| let maps = new Map();
maps.set("name", "ahui"); maps.set("phone", "17600888888");
maps.delete("name");
maps.has("name");
maps.get("name");
|
4. set 对象
set 里边不会有重复的值
1 2 3 4 5 6 7 8 9 10
| let sets = new Set();
sets.add(1); sets.add(2);
sets.delete(1);
sets.has(1);
sets.forEach((item, i) => {});
|
3. 模板字符串 ``
1 2 3 4 5 6 7
| const ahuiMsg = { name: "ahui", age: "18", phone: "17600888888" email: "ahuinet@163.com" } const intro = `大家好我叫${ahuiMsg.name},今年${ahuiMsg.age}`
|
2. 箭头函数 =>
- 能使用函数写法更简洁
- 函数返回值可以被隐式返回,不需要写 return
- 不重新绑定 this 的值
1. let const
let 定义变量
const 定义常量
var 可以重复定义变量,let const 不可以重复定义
var 函数作用域, let const 是块作用域
常用 const, 少用 let, 不用 var