From ae3c29858ffae9ba210b182402977e7664a7b34e Mon Sep 17 00:00:00 2001 From: jinphic <78671853+jinphic@users.noreply.github.com> Date: Thu, 4 Jan 2024 12:05:34 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=AE=97=E6=B3=95=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- interview/code/currying.js | 22 ++++++++++ interview/code/debounce.js | 37 +++++++++++++++++ interview/code/eventbus.js | 47 +++++++++++++++++++++ interview/code/mySetInterVal.js | 19 +++++++++ interview/code/promise.js | 74 +++++++++++++++++++++++++++++++++ interview/code/throttle.js | 16 +++++++ 6 files changed, 215 insertions(+) create mode 100644 interview/code/currying.js create mode 100644 interview/code/debounce.js create mode 100644 interview/code/eventbus.js create mode 100644 interview/code/mySetInterVal.js create mode 100644 interview/code/promise.js create mode 100644 interview/code/throttle.js diff --git a/interview/code/currying.js b/interview/code/currying.js new file mode 100644 index 0000000..86da6e6 --- /dev/null +++ b/interview/code/currying.js @@ -0,0 +1,22 @@ +function add() { + return arguments.reduce((prev, cur) => prev + cur, 0); + } + + + function currying(fn) { + let args = []; + return function temp() { + const context = this; + if (!arguments.length) { + let ans = fn.apply(context, args); + args = []; + return ans; + } else { + args = [ + ...args, + ...arguments, + ]; + return temp; + } + } + } \ No newline at end of file diff --git a/interview/code/debounce.js b/interview/code/debounce.js new file mode 100644 index 0000000..3a21634 --- /dev/null +++ b/interview/code/debounce.js @@ -0,0 +1,37 @@ +function debounce(fn, wait) { + let timer; + return function () { + if (timer) { + clearTimeout(timer); + } + timer = setTimeout(() => { + fn.apply(this, arguments); + }, wait); + } + } + + function debounce_immediate(fn, wait, immediate) { + let timer; + return function () { + let context = this; + let args = arguments; + + if (timer) { + clearTimeout(timer); + } + + if (!immediate) { + timer = setTimeout(() => { + fn.apply(context, args); + }, wait); + } else { + const callNow = !timer; + timer = setTimeout(() => { + clearTimeout(timer); + }, wait); + if (callNow) { + fn.apply(context, args); + } + } + } + } \ No newline at end of file diff --git a/interview/code/eventbus.js b/interview/code/eventbus.js new file mode 100644 index 0000000..4996b48 --- /dev/null +++ b/interview/code/eventbus.js @@ -0,0 +1,47 @@ +class EventBus { + static listener = new Map(); + + static on = (name, cb) => { + if (this.listener.has(name)) { + this.listener.get(name).push({ once: false, fn: cb }); + } else { + this.listener.set(name, [{ once: false, fn: cb }]); + } + } + + static once = (name, cb) => { + if (this.listener.has(name)) { + this.listener.get(name).push({ once: true, fn: cb }); + } else { + this.listener.set(name, [{ once: true, fn: cb }]); + } + } + + static off = (name, cb) => { + if (this.listener.has(name)) { + const cbs = this.listener.get(name).filter(item => item.fn !== cb); + this.listener.set(name, cbs); + } + } + + static removeAllListener = (name) => { + if (name) { + this.listener.delete(name); + } else { + this.listener.clear(); + } + } + + static emit = (name, ...rest) => { + if (this.listener.has(name)) { + const cache = this.listener.get(name); + cache.forEach(item => { + item.fn.call(this, rest); + }); + const newCache = cache.filter(item => !item.once); + this.listener.set(name, newCache); + } + } + + + } \ No newline at end of file diff --git a/interview/code/mySetInterVal.js b/interview/code/mySetInterVal.js new file mode 100644 index 0000000..992c6a7 --- /dev/null +++ b/interview/code/mySetInterVal.js @@ -0,0 +1,19 @@ +function mySetInterVal(fn, a, b) { + this.a = a; + this.b = b; + this.count = 0; + this.timer = null; + this.start = () => { + this.timer = setTimeout(() => { + fn(); + this.count++; + this.start(); + }, this.a + this.b * this.count); + } + this.stop = () => { + clearTimeout(this.timer); + this.count = 0; + } + } + + \ No newline at end of file diff --git a/interview/code/promise.js b/interview/code/promise.js new file mode 100644 index 0000000..b759bfa --- /dev/null +++ b/interview/code/promise.js @@ -0,0 +1,74 @@ +class MyPromise { + status = 'pending'; + data = undefined; + callbacks = []; + constructor(excutor) { + + const resolve = (value) => { + if (this.status !== 'pending') { + return; + } + this.status = 'resolved'; + this.data = value; + if (this.callbacks.length) { + this.callbacks.forEach(cb => { + cb.onResolved(value); + }) + } + } + + const reject = (reason) => { + if (this.status !== 'pending') { + return; + } + this.status = 'rejected'; + this.data = reason; + if (this.callbacks.length) { + this.callbacks.forEach(cb => { + cb.onRejected(reason); + }) + } + } + + + try { + excutor(resolve, reject) + } catch (error) {// 如果执行器抛出异常将失败 + console.error('🙅 error', error); + reject(error) + } + } + + + then(onResolved, onRejected) { + return new MyPromise((resolve, reject) => { + if (this.status === 'pending') { + this.callbacks.push({ onResolved, onRejected }); + } else if (this.status === 'resolved') { + try { + const res = onResolved(this.data); + if (res instanceof MyPromise) { + res.then( + value => resolve(value), + reason => reject(reason), + ); + } else { + resolve(res); + } + } catch (reason) { + reject(resolve); + } + } else { + onRejected(this.data) + } + }) + } + } + + + + var p = new MyPromise(function (resolve, reject) { resolve(1) }); + p.then( + res => console.log(res), + reason => console.error(reason) + ) \ No newline at end of file diff --git a/interview/code/throttle.js b/interview/code/throttle.js new file mode 100644 index 0000000..c39b9ce --- /dev/null +++ b/interview/code/throttle.js @@ -0,0 +1,16 @@ +function throttle(fn, wait) { + let timer; + return function () { + const context = this; + const args = arguments; + if (timer) { + return; + } + timer = setTimeout(() => { + fn.apply(context, args); + clearTimeout(timer); + }, wait); + } + } + + \ No newline at end of file