Skip to content

Instantly share code, notes, and snippets.

View linx4200's full-sized avatar

liuxinran linx4200

  • Shenzhen, China
View GitHub Profile
@linx4200
linx4200 / decorator.js
Created May 12, 2018 12:18
装饰器模式
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;
@linx4200
linx4200 / flyweight.js
Created May 12, 2018 12:55
享元模式,共享对象,减少内存消耗
const flyweight = () => {
// 已创建的元素
const created = [];
// 这个例子是共享 dom 元素
function create () {
const dom = document.createElement('div');
document.getElementById('container').appendChild(dom);
created.push(dom);
return dom;
}
@linx4200
linx4200 / sequence.js
Created June 25, 2018 09:03
依次(串行)执行多项异步任务
// method 1:
function sequence(tasks, fn) {
return tasks.reduce(
(promise, task) => promise.then(() => fn(task)),
Promise.resolve()
)
}
// method 2:
async function taskReducer(promise, action){
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;';
@linx4200
linx4200 / heap.js
Created May 19, 2019 05:51
JavaScript 实现一个 Heap (堆、完全二叉树、最小堆、最大堆)
function swap(arr, i, j) {
const temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
/* 堆是完全二叉树 */
/* 使用数组来实现完全二叉树 */
class Heap {
constructor(k) {
@linx4200
linx4200 / time-slicing.js
Created August 18, 2019 06:07
时间分片 Demo
// https://juejin.im/post/5d33fd0f5188256e820c80d4
function isFunction(func) {
return typeof func === 'function';
}
function isArray(arr) {
return Array.isArray(arr);
}