ikun

Results 18 comments of ikun

思路 - 如果 start year < end year 添加这一年剩下的月份 - 如果 start year = end year 添加差额的月份 ```javascript const getMounth = (start, end) => { const result = []; const...

最直接的思路是使用闭包 ```javascript const oddEvenConsole = () => { let count = 0; return function () { const isEven = count % 2 === 0; console.log(isEven ? 2 : 1); count++;...

```javascript const batchFetch = (urls = [], max = 3, callback) => { if (!callback) { throw new Error("Must set callback"); } /** * @param urlsLenght 总请求数量 * @param pendingFetchNums...

```javascript const printObject = (obj, cache = []) => { const keys = Object.keys(obj); keys.forEach(key => { if (typeof obj[key] === "object") { printObject(obj[key], cache); } else { cache.push(key); }...

```javascript function sleep(time) { return new Promise(resolve => { setTimeout(() => { resolve(); }, time); }); } function repeat(func, times, wait) { return async function wrappedFunction(...args) { for (let index...

```javascript function sortVersion(source) { function splitVersion(version) { return version.split("."); } function compareVersionPair(versionA, versionB) { const [aDetail, bDetail] = [splitVersion(versionA), splitVersion(versionB)]; let length = aDetail.length; let index = 0; console.log(`DEBUG: start...

cacheRequest 实现 ```javascript const cacheRequest = () => { const cache = new Map(); /** * 生成正确的 cache key * * @author Harper.Gao * @param {*} url * @param {*}...

```javascript class EventEmitter { callbacks = new Map(); on = (eventName, callback) => { if (!this.callbacks.has(eventName)) { return void this.callbacks.set(eventName, [callback]); } const currentEventCallbacks = this.callbacks.get(eventName); currentEventCallbacks.forEach(cb => { if...

- 创建一个全新的对象 - 执行 prototype 链接 - 绑定到函数调用的this - 如果没有返回对象则返回new出来的对象 ```javascript const myNew = (constructor, ...args) => { const obj = {}; Object.setPrototypeOf(obj, constructor.prototype); const result = constructor.call(obj, ...args); return...

```javascript function throttle(executer, wait, ...defaultParams) { let token = true; return function throttleWrappedExecuter(...args) { if (!token) { return; } token = false; setTimeout(() => { executer.call(this, ...defaultParams, ...args); token =...