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 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) { |
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
interface Computer { | |
run: () => void; | |
} | |
class Windows implements Computer { | |
run() { | |
console.log('Windows') | |
} | |
} |
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
class Computer { | |
constructor(mouse: string, keyboard: string, monitor: string) {} | |
} | |
class ComputerBuilder { | |
mouse: string; | |
keyboard: string; | |
monitor: string; | |
addMouse(mouse: string) { |
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 baiduMap = { | |
show() { | |
console.log('开始渲染百度地图。') | |
} | |
} | |
const renderMap = (map) => { | |
map.show() | |
} |
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() |
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
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 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
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 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; |
OlderNewer