GolderBrother

Results 44 comments of GolderBrother

function flattenAndSort(arr = []) { // 方法一: 借助concat方法 // function flatten(arr) { // while (arr.some((item) => Array.isArray(item))) { // arr = [].concat(...arr); // } // return arr; // } //...

```js // 第 1 题:写一个 mySetInterVal(fn, a, b),每次间隔 a,a+b,a+2b 的时间,然后写一个 myClear,停止上面的 mySetInterVal // export {}; class MySetInterval { constructor(fn, a, b) { this.a = a; this.b = b; this.fn =...

```js function handler(arr = []) { const result = []; for (let i = 0, len = arr.length; i < len - 1; i++) { for (let j = i...

```js ~function () { Array.prototype.myFlat = function (depth) { return this.reduce((accu, cur) => Array.isArray(cur) && depth > 1 ? accu.concat(cur.myFlat(depth - 1)) : accu.concat(cur), []); } Array.prototype.myFlat2 = function (depth)...

```js class EventEmitter { constructor(){ this.events = {}; } on(event, callback){ const callbacks = this.events[event] || []; if(Array.isArray(callbacks)) { callbacks.push(callback); this.events[event] = callbacks; } return this; } off(event, callback){ const...

```js // 给定起止日期,返回中间的所有月份 function getDate(dateStr = '', addMonth = 0) { const [year, month] = dateStr.split('-'); return new Date(year, month - 1 + addMonth); } function formateDate(dateStr = new Date())...

/** * 求斐波那契数列第n项的值 * @param {*} n 第n项 * @param {*} map 第n项的值,缓存计算结果,防止重复计算 */ function fibonacci(n, map = {}) { if (n === 1 || n === 2) return n;...

```js // 简单实现hooks // 一、实现useState const { render } = require("react-dom"); let memoriedStates = []; let lastIndex = 0; function useState(initialState) { memoriedStates[lastIndex] = memoriedStates[lastIndex] || initialState; function setState(newState) {...

## 区别 - 浅拷贝只复制一层对象的属性,并不包括对象里面的为引用类型的属性值,因此修改拷贝后的属性值是引用类型的,就会影响源对象 - 深拷贝就是对对象以及对象的所有子对象进行拷贝 ## 实现 ```js function cloneDeep(obj) { if(obj === null) return null; if (typeof obj !== 'object') return obj; if (obj instanceof Date) return new...

```js const url = "http://iauto360.cn/index.php?key0=0&key1=1&key2=2"; function parseParams(url) { if (!url) return null; const paramsObj = {}; const [, queryString] = url.split('?'); const queryStringArr = queryString.split('&'); for (const queryObj of queryStringArr)...