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 / debounce.js
Last active March 5, 2021 05:13
JavaScript debounce function.
function debounce(func, wait = 200, immediate) {
let timeout = null;
let context = null;
let args = null;
let previous = null;
let result = null;
function later() {
var passed = Date.now() - previous;
if (passed < wait) {
interface Computer {
run: () => void;
}
class Windows implements Computer {
run() {
console.log('Windows')
}
}
class Computer {
constructor(mouse: string, keyboard: string, monitor: string) {}
}
class ComputerBuilder {
mouse: string;
keyboard: string;
monitor: string;
addMouse(mouse: string) {
const baiduMap = {
show() {
console.log('开始渲染百度地图。')
}
}
const renderMap = (map) => {
map.show()
}
const robot = {
walk() {
console.log('walk')
}
}
robot.walk = (function() {
const originWalk = robot.walk
return function() {
originWalk()
@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);
}
@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 / 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 / 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 / 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;