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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
| class People { constructor(name) { this.name = name this.eventMap = {} }
on(eventName, fn) { if (this.eventMap[eventName]) { this.eventMap[eventName].push(fn) } else { this.eventMap[eventName] = fn ? [fn] : [] } } off(eventName, fn) { if (!this.eventMap[eventName]) { return } const fnIndex = this.eventMap[eventName].findIndex(item => item === fn) if (fnIndex !== -1) { this.eventMap[eventName].splice(fnIndex, 1) } } emit(eventName, ...args) { if (Array.isArray(this.eventMap[eventName])) { this.eventMap[eventName].forEach(fn => fn(...args)) } }
sayHi() { console.log(`Hi, I am ${this.name}`) } }
const say1 = (greeting) => { console.log(`${greeting}, nice meeting you.`) }
const say2 = (greeting) => { console.log(`${greeting}, nice meeting you, too.`) }
const jerry = new People('Jerry') jerry.sayHi()
jerry.on('greeting', say1) jerry.on('greeting', say2)
jerry.emit('greeting', 'Hi')
jerry.off('greeting', say1) jerry.emit('greeting', 'Hi')
const sleep = (duration) => { return new Promise((resolve, reject) => { let timeout = null; try { timeout = setTimeout(() => { resolve(true) clearTimeout(timeout); }, duration) } catch (e) { reject(e) } }) }
const anyFunc = async () => { console.log("123") await sleep(300) console.log("456") }
anyFunc()
const deepGet = (obj, prop) => { const regex = /\[(\d+)\]/g const replacedStr = prop.replace(regex, '.$1') const paramList = replacedStr.split('.') for (let i = 0; i < paramList.length; i++) { if (!isNaN(paramList[i])) { paramList[i] = Number(paramList[i]) } } const iterate = (curObj, keyList, keyIndex) => { const newObj = curObj[keyList[keyIndex]] ?? undefined if (newObj === undefined || keyList.length <= ++keyIndex) { console.info(newObj) return newObj } else { iterate(newObj, keyList, keyIndex) } } return iterate(obj, paramList, 0) }
deepGet({ school: { student: { name: 'Tomy' }, }, }, 'school.student.name')
deepGet({ school: { students: [ { name: 'Tomy' }, { name: 'Lucy' }, ], } }, 'school.students[1].name')
deepGet({ user: { name: 'Tomy' } }, 'user.age') deepGet({ user: { name: 'Tomy' } }, 'school.user.age')
const combo = (...args) => { const fnList = args.reverse() const iterate = (list, index, param) => { const res = list[index](param) if (list.length > ++index) { return iterate(list, index, res) } else { return res } } return function (value) { return iterate(fnList, 0, value) } }
const addOne = (a) => a + 1 const multiTwo = (a) => a * 2 const divThree = (a) => a / 3 const toString = (a) => a + '' const split = (a) => a.split('')
split(toString(addOne(multiTwo(divThree(666)))))
const testForCombo = combo(split, toString, addOne, multiTwo, divThree) const testForComboRes = testForCombo(666) console.info(testForComboRes)
|