Js的进阶使用

TJF.

Array Methods Advanced

1. reduce’s useage

1
2
3
4
5
6
7
8
9
10
// sum
let tempArr = [1,2,3,4,5];
temp tempArr.reduce((pre,cur)=>pre+cur,1); // res -> 22

// get the minvalue from the array
let tempArr = [1,2,3,4,5];
temp tempArr.reduce((pre,cur)=>pre>cur?pre:cur); // res -> 5

// get the maxValue from the array
temp tempArr.reduce((pre,cur)=>pre<cur?pre:cur); // res -> 5

2. Array.filter

1
2
3
// filter the meaningless data in the array   such as "", undefined , 0 , null ,false
let arr = [3, 0, 6, 7, "", false];
arr.filter(Boolean);

3. common Object methods

1
2
3
4
5
6
7
8
9
10
const arr = { a: 1, b: 2, c: 3 };

// Object.entries
console.log(Object.entries(arr)); // res -> [["a",1],["b",2],["c",3]]

// Object.keys
console.log(Object.keys(arr)); // res-> ["a","b","c"]

// Object.values
console.log(Object.values(arr)); // res-> [1,2,3]

MAP AND SET IN ES6

1

Js 中的隐式转换规则(ToPrimitive)

1
2
3
调用 ToPrimitive() 内部函数,将对象转换为字符串,然后两者进行比较。在 js 中,想要将对象转换成原始值,必然会调用 toPrimitive() 内部函数。


OTHERS

1. the diffrence between “for in “ and “ for of”

1
2
3
4
5
6
7
8
9
10
11
// `for..in`迭代的是对象的键的列表(下标),而 `for..of`则迭代对象的键对应的值 (对应值)
let list = [1, 2, 3];
for (i of list) {
console.log(i);
} // res-> [1,2,3]

for (i in list) {
console.log(i);
} // res-> [0,1,2]

// 注: Set和Map结构中,由于没有下标的存在 所以无法使用for in; 可以使用for of来迭代

2. some Operator in js

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
28
29
30
31
//       ?.   可选链操作符  =====> 引用连接的值为空不会短路报错;
// ?? 空值合并操作符
// || 逻辑操作符 或



One ?? Two
One || Two

异同:
1)使用??时,Onenull 或者undefined 时,使用Two的值;
2)使用|| 时, 先把One转成Bollean值进行判断,为假时使用Two的值;

EG:
 // ??
  undefined ?? 2    // 2
  null ?? 2        // 2
  0 ?? 2            // 0
  "" ?? 2            // ""
  true ?? 2        // true
  false ?? 2        // false


 // ||
  undefined || 2    // 2
  null || 2        // 2
  0 || 2            // 2
  "" || 2            // 2
  true || 2        // true
  false || 2        // 2

非空断言操作符

1
2
3
4
5
// 这是TypeScript的语法,叫非空断言操作符(non-null assertion operator),和?.相反,这个符号表示对象后面的属性一定不是null或undefined。

let name: string = "kite";

console.log(name!.trim());

空值赋值运算符 (??=)

mai

3 object 中 delete 的用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
知识点
1.delete使用原则:delete 操作符用来删除一个对象的属性。
2.delete在删除一个不可配置的属性时在严格模式和非严格模式下的区别:
1)在严格模式中,如果属性是一个不可配置(non-configurable)属性,删除时会抛出异常;
2)非严格模式下返回 false
3.delete能删除隐式声明的全局变量:这个全局变量其实是global对象(window)的属性
4.delete能删除的:
1)可配置对象的属性(2)隐式声明的全局变量 (3)用户定义的属性 (4)在ECMAScript 6中,通过 constlet 声明指定的 "temporal dead zone" (TDZ) 对 delete 操作符也会起作用
delete不能删除的:
1)显式声明的全局变量 (2)内置对象的内置属性 (3)一个对象从原型继承而来的属性
5.delete删除数组元素:
1)当你删除一个数组元素时,数组的 length 属性并不会变小,数组元素变成undefined
2)当用 delete 操作符删除一个数组元素时,被删除的元素已经完全不属于该数组。
3)如果你想让一个数组元素的值变为 undefined 而不是删除它,可以使用 undefined 给其赋值而不是使用 delete 操作符。此时数组元素是在数组中的
6.delete 操作符与直接释放内存(只能通过解除引用来间接释放)没有关系。

4 CommonJs 中 import 和 require 的区别

1
2
1.import 的命令是在编译阶段执行,导入的模块会先运行; require的命令是按需加载导入;

原型和原型链

1
2
3
4
5
6
原型链是一种查找过程,当一个对象访问属性及方法时,先查找自身是否具有这个属性,如果没有则是通过自身的_proto_去查找上层的prototype,直至查找到或者到顶层的Objectnull;

// prototype
prototype 定义为对象的原型,在prototype上挂载的方法,会在实例化的时候,给到实例对象;
constructor是prototype上的一个属性;
person.prototype.constructor = person;

闭包的使用

1
// 函数内部返回一个函数 可以实现私有变量的保护

JSON.stringfy 的缺点

1
2
3
// JSON.stringfy 将对象转换字符
// 缺点:对象中含有undefined,函数等会丢失,NAN和undefined的数据会转换成undefined;
//
  • 标题: Js的进阶使用
  • 作者: TJF.
  • 创建于 : 2022-09-16 00:00:00
  • 更新于 : 2024-03-04 08:40:28
  • 链接: https://github.com/taowind/2022/09/16/JS 进阶使用/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论