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); | |
} |
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
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
// 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
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
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
var LazySingleton = (function() { | |
// 单例实例引用 | |
var instance = null; | |
// 单例 | |
function Singleton() { | |
return { | |
publicMethod: function() {}, | |
publicProperty: '1' | |
} | |
} |
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 A = { | |
Util: { | |
util_method1: function() {}, | |
util_method2: function() {}, | |
// ... | |
}, | |
Tool: { | |
tool_method1: function() {}, | |
tool_method2: function() {}, | |
// ... |
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 Factory = function(type, content) { | |
if (this instanceof Factory) { | |
return new this[type](content); | |
} else { | |
return new Factory(type, content); | |
} | |
} | |
// 工厂原型中设置创建所有类型数据对象的基类 |
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 BasketBall = function () { /* ... */}; | |
var FootBall = function () { /* ... */}; | |
var Tennis = function () { /* ... */}; | |
// 运动工厂 | |
var sportsFactory = function (name) { | |
switch(name) { | |
case 'NBA': | |
return new BasketBall(); | |
case 'WorldCup': |
NewerOlder