03.【JavaScript】
fans0414
这个作者很懒,什么都没留下…
展开
-
JavaScript 防抖
const input = document.getElementById("input"); let timer = null; input.addEventListener('keyup',e => { if(timer){ clearTimeout(timer); } timer = setTimeout(() => { timer = null; console.log(e.target.value) .原创 2021-09-02 18:00:41 · 61 阅读 · 0 评论 -
JavaScript 实现栈结构
class Stack{ constructor(){ this.data = []; this.count = 0; } push(value){ this.data[this.count] = value; this.count++; } pop(){ if(this.isEmpty()){ console.log('栈为空') ret.原创 2021-08-18 16:56:33 · 53 阅读 · 0 评论 -
JavaScript 实现观察者模式
示例代码 class Dep { constructor(){ this.subs = []; } addSub(sub){ if(sub && sub.update){ this.subs.push(sub) } } notify(){ this.subs.forEach(sub => { sub.update()原创 2021-08-17 16:04:32 · 71 阅读 · 0 评论 -
JavaScript 实现发布订阅模式
示例代码: class EventEmitter { constroctor(){ this.subs = {}; } $on(eventType,handler){ this.subs[eventType] = this.subs[eventType] || []; this.subs[eventType].push(handler) } $emit(eventType){ this.subs[e原创 2021-08-17 15:53:23 · 106 阅读 · 0 评论