This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function shuffle(arr) { | |
if (!arr || arr.length === 0) { | |
return []; | |
} | |
if (arr.length === 1) { | |
return [...arr]; | |
} | |
const map = new Map(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>Concurrent request</title> | |
</head> | |
<body> | |
<button id="fetch">fetch</button> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createStore(state, reducer) { | |
const listeners = []; | |
const getState = () => state; | |
const dispatch = (action) => { | |
state = reducer(state, action); | |
listeners.forEach((item) => item()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Promise A+: https://promisesaplus.com/ | |
function MyPromise(fn) { | |
this.status = 'pending'; | |
this.value = null; | |
this.callbacks = []; | |
this.resolve = (newVal) => { | |
const fn = () => { | |
if (this.status !== 'pending') { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function uniqueWithSet(arr) { | |
return Array.from(new Set(arr)); | |
} | |
function uniqueWithMap(arr) { | |
const map = {}; | |
const res = []; | |
for (item of arr) { | |
// 主要为了区分数字与字符串,例如数字 1 和字符串 '1' | |
const typeSign = typeof item; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const arr = [1, [2, [3], [4]]]; | |
arr.flat(Infinity); | |
function _flat(arr) { | |
return ( | |
Array.isArray(arr) | |
? arr.reduce((acc, cur) => [...acc, ..._flat(cur)], []) | |
: [arr] | |
) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function myInstanceOf(instance, constructor) { | |
if (typeof constructor !== 'function') { | |
throw Error(`The parameter 'constructor' is not a function!`) | |
} | |
let proto = instance.__proto__; | |
const prototype = constructor.prototype; | |
while (proto !== null) { | |
if (prototype === proto) { | |
return true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function quickSort(list) { | |
return sortCore(list, 0, list.length - 1); | |
} | |
function sortCore(list, first, last) { | |
if (last <= first) { | |
return list; | |
} | |
const povitIndex = partition1(list, first, last); | |
// const povitIndex = partition2(list, first, last); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function heapSort(arr) { | |
for (let i = (arr.length >> 1) - 1; i >= 0; i--) { | |
adjust(arr, i, arr.length); | |
} | |
for (let j = arr.length - 1; j >= 0; j--) { | |
swap(arr, 0, j); | |
adjust(arr, 0, j); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const robot = { | |
walk() { | |
console.log('walk') | |
} | |
} | |
robot.walk = (function() { | |
const originWalk = robot.walk | |
return function() { | |
originWalk() |
NewerOlder