Data Structures, Modern Operators and Strings
基础的语法,基础的Javascript功能

1. Destructuring Arrays 解构数组

destructing is an ES6 feature
and it’s basically a way of unpacking values 实际上就是一种给值解包的方式
from an array or an object into seperatate variables. 把数组或者对象拆分成单独的变量
把数组或者对象解包成单独的值
destructing is to break a complex data structure down
into a smaller data structure like a variable
解析就是把复杂的结构【数组、对象】分解为简单的结构【变量】

解析数组

  1. const arr = [2, 3, 4];
  2. // 之前的检索数组的方式
  3. const a = arr[0];
  4. const b = arr[1];
  5. const c = arr[2];
  6. console.log(a, b, c);
  7. // 解析的方式
  8. const [x, y, z] = arr; //看起来是数组,其实是完成解析任务;所以看到 [] = 就是在解析; 不要忘记使用 const 声明变量
  9. console.log(x, y, z);

image.png

取变量

  1. // 按照顺序取出变量
  2. console.log(restaurant.categories);
  3. const [first, second] = restaurant.categories;
  4. console.log(first, second);
  5. // 取出第一个和第三个
  6. const [first1, , third] = restaurant.categories;
  7. console.log(first1, third);

image.png

交换变量

  1. //交换变量
  2. console.log('------switching variables------');
  3. let [main, , secondary] = restaurant.categories;
  4. console.log(main, secondary);
  5. // 之前交换变量的方式
  6. // const temp = main;
  7. // main = secondary;
  8. // secondary = temp;
  9. // console.log(main, secondary);
  10. // 使用 解析的方式
  11. [main, secondary] = [secondary, main];
  12. console.log(main, secondary);

image.png

解析函数返回的数组

  1. ...
  2. order: function (starterIndex, mainIndex) {
  3. return [this.starterMenu[starterIndex], this.mainMenu[mainIndex]];
  4. },
  5. ...
  6. //解析 函数返回的数组
  7. console.log('------constructing the array from function------');
  8. const [starter, mainM] = restaurant.order(0, 2);
  9. console.log(starter, mainM);

image.png

嵌套的数组

  1. // 嵌套的数组 nested array
  2. console.log('------nested array------');
  3. const nested = [2, 4, [5, 6]];
  4. const [i, , j] = nested;
  5. console.log(i, j);
  6. //在解析里面继续解析
  7. const [o, , [p, q]] = nested;
  8. console.log(o, p, q);

image.png

默认值

  1. // 设置默认值
  2. console.log('------default values------');
  3. // const [r, s, t] = [8, 9]; //8 9 undefined
  4. const [r = 1, s = 1, t = 1] = [8, 9];
  5. console.log(r, s, t);

image.png

2. Destructing Objects 解析对象

解析对象

  1. // 解析对象, 不需要注意【顺序】,只要 【属性名称】 对就行
  2. console.log('------ destructing objects ------');
  3. const { restaurant_name, openingHours, categories } = restaurant;
  4. console.log(restaurant_name, openingHours, categories);

image.png

给属性重命名

  1. console.log('------ rename property ------');
  2. const {
  3. restaurant_name: restaurant_Name,
  4. openingHours: hours,
  5. categories: tags,
  6. } = restaurant;
  7. console.log(restaurant_Name, hours, tags);

image.png

默认值

  1. // 默认值,当要读取的对象属性不存在时候,很重要
  2. console.log('------ default value ------');
  3. // const { menu, starterMenu: starters = [] } = restaurant; //undefined
  4. const { menu = [], starterMenu: starters = [] } = restaurant;
  5. console.log(menu, starters);

image.png

mutating variable

  1. // mutating variables 改变变量
  2. // {} 是代码块不能直接赋值操作 要加 ()
  3. console.log('------ mutating variables ------');
  4. let a = 1;
  5. let b = 1;
  6. const obj = { a: 28, b: 39, c: 12 };
  7. ({ a, b } = obj);
  8. console.log(a, b);

image.png
{a, b} = obj;
image.png

嵌套对象

  1. // nested objects 嵌套的对象
  2. console.log('------ nested objects ------');
  3. const { fri } = restaurant.openingHours;
  4. console.log(fri);
  5. const {
  6. fri: { open: o, close: c },
  7. } = restaurant.openingHours;
  8. console.log(o, c);

image.png

对象作为函数参数

  1. ...
  2. orderDelivery: function ({
  3. starterIndex = 1,
  4. mainIndex = 0,
  5. time = '20:00',
  6. address,
  7. }) {
  8. console.log(
  9. `Order received! ${this.starterMenu[starterIndex]} and ${this.mainMenu[mainIndex]} will be delived to ${address} at ${time}`
  10. );
  11. }
  12. ...
  13. restaurant.orderDelivery({
  14. time: '22:30',
  15. addrss: 'NY',
  16. mainIndex: '2',
  17. starterIndex: '2',
  18. });

image.png

3. The Spread Operator 传播运算符

扩展数组

  1. // 扩展数组
  2. const arr = [7, 8, 9];
  3. const badNewArr = [1, 2, arr[0], arr[1], arr[2]];
  4. console.log(badNewArr);
  5. const newArr = [1, 2, arr];
  6. console.log(newArr);
  7. const newArr1 = [1, 2, ...arr];
  8. console.log(newArr1);

image.png

数组扩展

  1. //扩展数组
  2. const newMenu = [...restaurant.mainMenu, 'Gnocci'];
  3. console.log(newMenu);

image.png

复制数组

  1. // 复制数组 这是浅拷贝
  2. console.log('------ copy array ------');
  3. const mainMenuCopy = [...restaurant.mainMenu];
  4. console.log(mainMenuCopy);

image.png

连接两个数组

  1. // 连接两个数组
  2. const mainMenuJoint = [...restaurant.mainMenu, ...restaurant.mainMenu];
  3. console.log(mainMenuJoint);

image.png

作用于Iterables

  1. // Iterables: arrays, strings, sets, maps, NOT objects
  2. console.log('------ Iterables ------');
  3. const str = 'Jonas';
  4. const letters = [...str, ' ', 'S.'];
  5. console.log(letters);
  6. console.log(...str);
  7. //console.log(`${...str} Schmdtman`);

image.png
spread operator works on all so-called iterables
应用于所有可迭代对象
array, string, set, map, NOT object

  1. console.log(`${...str} Schmdtman`);

image.png
只有当我们传递参数到函数里面,或者构建一个新数组的时候使用

在函数传参时候使用

  1. ...
  2. orderPasta: function (ing1, ing2, ing3) {
  3. console.log(`you get a pasta with ${ing1}, ${ing2} and ${ing3}`);
  4. },
  5. ...
  6. // 扩展操作符在函数传参时候使用
  7. console.log('------ 扩展操作符在函数传参时候使用 ------');
  8. const ingredients = [
  9. prompt("Let's make pasta! Ingredient 1?"),
  10. prompt('Ingredient 2?'),
  11. prompt('Ingredient 3'),
  12. ];
  13. console.log(ingredients);
  14. restaurant.orderPasta(ingredients[0], ingredients[1], ingredients[2]);
  15. restaurant.orderPasta(...ingredients);

image.png

使用在object上

ES2018 后可以使用在了 object

  1. // Object
  2. const newRestaurant = { foundedIn: 1998, ...restaurant, foounder: 'Mark' };
  3. console.log(newRestaurant);

image.png

4. Rest pattern and Parameters

与 spread operator 相反
unpack an array
pack elements into an array
put the rest elments packed into an array

  1. // spread, because on the Right
  2. const arr = [1, 2, ...[3, 4]];
  3. // rest, because on the left
  4. const [a, b, ...others] = [1, 2, 3, 4, 5];
  5. console.log(a, b, others);

image.png

  1. /// functions
  2. console.log('------ add function ------');
  3. const add = function (...numbers) {
  4. let sum = 0;
  5. for (let i = 0; i < numbers.length; i++) {
  6. sum += numbers[i];
  7. }
  8. console.log(sum);
  9. };
  10. add(2, 3);
  11. add(1, 2, 3, 4, 5);
  12. const x = [2, 4, 6, 8];
  13. add(...x);

image.png

5. Short Circuiting(&& and ||)

AND &&
OR || 如果第一个是true的值,就返回这个值,不进行后面的操作;
如果第一个不是,往后找,找到为true的值,返回这个值,然后不再进行后面的操作
  1. // use ANY data type, return ANY data type, short-circuiting
  2. console.log(3 || 'Jonas');
  3. console.log('' || 'Mark');
  4. console.log(true || 0);
  5. console.log(undefined || null);
  6. console.log(undefined || 0 || '' || 'hello' || 23 || null);

image.png

  1. console.log(0 && 'Jonas');
  2. console.log(7 && 'Jonas');
  3. console.log('hello' && 'Jonas' && 'there' && null);

image.png

6. The Nullish coalescing operator(??)

ES 2020
nullish :
null and undefine
NOT 0 or ‘’

7. Logical Assignment Operators

  1. const rest1 = {
  2. name: 'Capi',
  3. numGuests: 20,
  4. };
  5. const rest2 = {
  6. name: 'La Piazza',
  7. owner: 'Mark',
  8. };
  9. // OR assignment operator
  10. // rest1.numGuests = rest1.numGuests || 10;
  11. // rest2.numGuests = rest2.numGuests || 10;
  12. rest1.numGuests ||= 10;
  13. rest2.numGuests ||= 10;
  14. console.log(rest1);
  15. console.log(rest2);

image.png
但是 如果 rest1.numGuests = 0 , || 会把 0 判断为false

  1. rest1.numGuests ??= 10;
  2. rest2.numGuests ??= 10;

image.png

8. Coding Challenge 1

9. Looping Arrays The for-of loop

  1. const menu = [...restaurant.starterMenu, ...restaurant.mainMenu];
  2. console.log(menu);
  3. for (const item of menu) {
  4. console.log(item);
  5. }

image.png

  1. for (const item of menu.entries()) {
  2. console.log(item);
  3. }

image.png
看看 menu.entries() 是什么
是一个数组迭代器

  1. console.log(menu.entries()); //Array Iterator {}

image.png
什么是数组迭代器,展开看一看
是7个数组,每个数组有两个元素,一个索引号,一个值

  1. console.log([...menu.entries()]);

image.png
item 本身是一个数组,所以可以用spread array 分别得到 索引号和值

  1. for (const [index, element] of menu.entries()) {
  2. console.log(`${index}: ${element}`);
  3. }

image.png

10. Enhanced Object Literals 增强的对象字面值

11. Optional chaining 可选链接

image.pngimage.png