From ff881a0e213bc330ccbc03f69250514e4b02076e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E5=89=8DHell?= <542168513@qq.com> Date: Mon, 2 Sep 2024 15:23:36 +0800 Subject: [PATCH] =?UTF-8?q?[feat]=20=E6=9B=B4=E6=96=B0=20record?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/blog/_meta.json | 5 ++++ docs/blog/list/magicai.md | 56 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 docs/blog/list/magicai.md diff --git a/docs/blog/_meta.json b/docs/blog/_meta.json index 03fab041..fd4f727e 100644 --- a/docs/blog/_meta.json +++ b/docs/blog/_meta.json @@ -4,6 +4,11 @@ "name": "index", "label": "原创笔记" }, + { + "type": "dir", + "name": "list", + "label": "一点记录" + }, { "type": "file", "name": "taro_3", diff --git a/docs/blog/list/magicai.md b/docs/blog/list/magicai.md new file mode 100644 index 00000000..ec7066cb --- /dev/null +++ b/docs/blog/list/magicai.md @@ -0,0 +1,56 @@ + +# 0902面试总结 + +## 手写题 + +1. usePrevious + +```typescript +import { useEffect, useRef } from 'react'; + +export default function usePrevious(state: T): T | undefined { + const ref = useRef(); + + useEffect(() => { + ref.current = state; + }, [state]); + + return ref.current; +} +``` + +2. LazyMan 类实现 + +```javascript + +class LazyMan { + constructor(name) { + this.name = name; + console.log(name); + + this.base = Promise.resolve() + } + + sleep (time) { + this.base = this.base.then(() => { + return new Promise((resolve) => { + console.log(`等待${time}秒`) + setTimeout(() => { + resolve() + }, time * 1000); + }) + }) + return this; + } + + eat (food) { + this.base = this.base.then(() => { + console.log(`正在吃${food}`) + }) + return this; + } +} + +new LazyMan('张三').eat('banana').sleep(3).eat('launch').sleep(4).eat('food'); + +``` \ No newline at end of file