Skip to content

Instantly share code, notes, and snippets.

View zhongyangxun's full-sized avatar
🦄
Slow down

zhongyangxun zhongyangxun

🦄
Slow down
View GitHub Profile
@zhongyangxun
zhongyangxun / shuffle.js
Created April 14, 2021 16:12
数组乱序。
function shuffle(arr) {
if (!arr || arr.length === 0) {
return [];
}
if (arr.length === 1) {
return [...arr];
}
const map = new Map();
@zhongyangxun
zhongyangxun / concurrent-request.html
Created April 3, 2021 15:39
限制并发请求数量的方法。
<!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>
@zhongyangxun
zhongyangxun / myRedux.js
Created March 24, 2021 14:39
手写简易 Redux
function createStore(state, reducer) {
const listeners = [];
const getState = () => state;
const dispatch = (action) => {
state = reducer(state, action);
listeners.forEach((item) => item());
}
@zhongyangxun
zhongyangxun / MyPromise.js
Last active March 13, 2021 13:07
手写 Promise.
// 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') {
@zhongyangxun
zhongyangxun / arr-unique.js
Created January 22, 2021 09:39
花式数组去重
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;
@zhongyangxun
zhongyangxun / flat.js
Last active January 22, 2021 09:11
花式数组扁平化
const arr = [1, [2, [3], [4]]];
arr.flat(Infinity);
function _flat(arr) {
return (
Array.isArray(arr)
? arr.reduce((acc, cur) => [...acc, ..._flat(cur)], [])
: [arr]
)
}
@zhongyangxun
zhongyangxun / myInstanceOf.js
Created January 21, 2021 15:24
Simulate the `instanceof` keyword.
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;
@zhongyangxun
zhongyangxun / quickSort.js
Created January 15, 2021 07:34
Quick sort algorithm based on JavaScript.
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);
@zhongyangxun
zhongyangxun / heapSort.js
Last active January 15, 2021 07:33
Heap sort algorithm based on JavaScript.
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);
}
const robot = {
walk() {
console.log('walk')
}
}
robot.walk = (function() {
const originWalk = robot.walk
return function() {
originWalk()