nmsn

Results 78 comments of nmsn

### target target 表示编辑结果的标准,作为某些特性的降级还是保留标准 > Default: es3 > Allowed: es3/es5/es6(es2015)/es2016...2022/esnext 相关属性 [lib](https://www.typescriptlang.org/tsconfig#lib) 选择 target 会自动引入相关lib 作为新特性的补充,也可手动配置 例如: 高级库: - es6(es2015): 增加了 array.find/Promise/Proxy/Symbol/Map/Set/Reflect 等 - es7(es2016): 增加了 array.includes 等 - es2017:...

js 实现文本省略号方案的基本原理为: 二分法找到文本在容器边缘的字符,后面字段截断。 思路简单,但其中涉及比较多注意和需要优化的点。

```ts class AsyncLimit { limit: number; count: number; queue: any[]; constructor(n: number) { this.limit = n; this.count = 0; this.queue = []; } enqueue(fn: () => unknown) { // 关键代码:...

IOC(Inversion of Control,控制反转)是一种设计模式,它将控制权从调用者转移到被调用者,从而实现解耦和灵活性。在传统的程序设计中,程序员通常会直接调用其他模块或对象的方法,这种方式称为“主动式控制”。而在 IOC 中,程序员不再直接调用其他模块或对象的方法,而是将控制权交给框架或容器,由它们来调用相应的方法,这种方式称为“被动式控制”。通过使用 IOC,可以实现模块之间的松耦合,增强程序的可维护性和可扩展性。在实际开发中,常见的 IOC 框架包括 Spring、Guice 等。

```js Promise.resolve(1).then(2).then(Promise.resolve(3)).then(console.log) ``` 执行结果 `1` then 方法传入非函数会透传

```js const promise = Promise.resolve().then(() => { return promise }); ``` 会报错 then/catch 不能返回 promise 本身,否则会造成死循环

```js Promise.resolve(1) .then(res => { console.log(res) }) .finally(() => console.log('finally')); Promise.resolve(2) .finally(() => { console.log('finally2'); return '我是 finally2 返回的数'; }) .then(res => { console.log('finally2 后面的 then 函数', res); }); ```...

```js function runAsync(x) { const p = new Promise(r => setTimeout(() => r(x, console.log(x)), 1000)); return p; } function runReject(x) { const p = new Promise((res, rej) => setTimeout(() =>...

```js async function async1() { console.log('async start'); await async2(); console.log('async1 end'); setTimeout(() => { console.log('timer1'); }, 0); } async function async2() { setTimeout(() => { console.log('timer2'); }, 0); console.log('async2'); }...

```js const p1 = new Promise((resolve) => { setTimeout(() => { resolve('resolve3'); console.log('timer1'); }, 0); resolve('resolve1'); resolve('resolve2'); }).then(res => { console.log(res); setTimeout(() => { console.log(p1); }, 1000); }).finally(res => {...