This file contains hidden or 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 decorator = (input, fn) => { | |
const input = document.getElementById(input); | |
if (typeof input.onclick === 'function') { | |
const oldClickFn = input.onclick; | |
input.onclick = () => { | |
oldClickFn(); | |
fn(); | |
} | |
} else { | |
input.onclick = fn; |
This file contains hidden or 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 flyweight = () => { | |
// 已创建的元素 | |
const created = []; | |
// 这个例子是共享 dom 元素 | |
function create () { | |
const dom = document.createElement('div'); | |
document.getElementById('container').appendChild(dom); | |
created.push(dom); | |
return dom; | |
} |
This file contains hidden or 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
// method 1: | |
function sequence(tasks, fn) { | |
return tasks.reduce( | |
(promise, task) => promise.then(() => fn(task)), | |
Promise.resolve() | |
) | |
} | |
// method 2: | |
async function taskReducer(promise, action){ |
This file contains hidden or 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
var decoder = (function () { | |
var canvas, ctx; | |
var init = function () { | |
var list = document.getElementsByClassName('encrypted'); | |
for (var i = 0; i < list.length; i++) { | |
var entry = list[i]; | |
var wrapper = document.createElement('div'); | |
wrapper.className = 'imagesafe'; | |
var style = entry.style; | |
var pos = 'float:' + (style.float || style.cssFloat) + ';left:' + style.left + 'px;top:' + style.top + 'px;'; |
This file contains hidden or 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 swap(arr, i, j) { | |
const temp = arr[i]; | |
arr[i] = arr[j]; | |
arr[j] = temp; | |
} | |
/* 堆是完全二叉树 */ | |
/* 使用数组来实现完全二叉树 */ | |
class Heap { | |
constructor(k) { |
This file contains hidden or 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
// https://juejin.im/post/5d33fd0f5188256e820c80d4 | |
function isFunction(func) { | |
return typeof func === 'function'; | |
} | |
function isArray(arr) { | |
return Array.isArray(arr); | |
} |
OlderNewer