Skip to content

Instantly share code, notes, and snippets.

View linx4200's full-sized avatar

liuxinran linx4200

  • Shopee
  • Shenzhen, China
View GitHub Profile
@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);
}
@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) {
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 / 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){
@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 / 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 / patterns-factory-lazy-singleton.js
Created March 17, 2018 15:09
【Factory】惰性单例
var LazySingleton = (function() {
// 单例实例引用
var instance = null;
// 单例
function Singleton() {
return {
publicMethod: function() {},
publicProperty: '1'
}
}
@linx4200
linx4200 / patterns-factory-singleton.js
Created March 17, 2018 15:05
【Factory】单例模式
var A = {
Util: {
util_method1: function() {},
util_method2: function() {},
// ...
},
Tool: {
tool_method1: function() {},
tool_method2: function() {},
// ...
@linx4200
linx4200 / patterns-factory-safe-factory-method.js
Created March 17, 2018 14:13
【Factory】安全的工厂方法
// 安全模式创建的工厂类
var Factory = function(type, content) {
if (this instanceof Factory) {
return new this[type](content);
} else {
return new Factory(type, content);
}
}
// 工厂原型中设置创建所有类型数据对象的基类
@linx4200
linx4200 / patterns-factory-simple.js
Created March 17, 2018 12:51
【Factory】最简单的工厂模式
var BasketBall = function () { /* ... */};
var FootBall = function () { /* ... */};
var Tennis = function () { /* ... */};
// 运动工厂
var sportsFactory = function (name) {
switch(name) {
case 'NBA':
return new BasketBall();
case 'WorldCup':