Skip to content

Instantly share code, notes, and snippets.

View LeeeeeeM's full-sized avatar
🎯
专心研究webpack的细节。

EMpersonal LeeeeeeM

🎯
专心研究webpack的细节。
View GitHub Profile
@LeeeeeeM
LeeeeeeM / closure.js
Last active February 12, 2018 03:02
closure for trace
const event1 = 1;
const event2 = 2;
function trace(event, keypress = () => []) {
return function trace() {
return [...keypress(), event];
}
}
let keypress = trace(event1);
@LeeeeeeM
LeeeeeeM / throttle.js
Created February 12, 2018 08:36
throttle
function throttle(fn, delay, cxt) {
var start;
var timer = null;
cxt = cxt || this;
delay = delay || 100;
return function() {
var current = Date.now();
clearTimeout(timer);
if (!start) {
start = current;
@LeeeeeeM
LeeeeeeM / compose.js
Created February 14, 2018 05:04
组合函数
function compose(fn) {
var fn = [].slice.call(arguments);
return function(value) {
while(fn.length > 0) {
value = fn.pop()(value);
}
return value;
}
}
@LeeeeeeM
LeeeeeeM / compose.js
Last active February 14, 2018 05:15
组合函数使用reduce
function compose(fn) {
var fnArray = [].slice.call(arguments);
return function(value) {
return fnArray.reverse().reduce(function(result, fn) {
return fn(result)
}, value);
};
}
@LeeeeeeM
LeeeeeeM / compose.js
Last active February 14, 2018 13:41
compose每次调用都传入新函数
function compose(fn) {
var fnArray = [].slice.call(arguments);
return fnArray.reverse().reduce(function(fgr, fn) {
// 因为传入的是一个函数,最终输出一个函数 fgr =====> function
return function() {
var args = [].slice.call(arguments);
return fn(fgr.apply(null, args));
};
});
}
@LeeeeeeM
LeeeeeeM / compose.js
Created February 14, 2018 15:07
递归实现compose
function compose(fn) {
// 传入的函数大于等于2
var fnArray = [].slice.call(arguments).reverse();
var fn1 = fnArray[0];
var fn2 = fnArray[1];
if (fnArray.length > 2) {
var restArray = fnArray.slice(2);
@LeeeeeeM
LeeeeeeM / curry.js
Created February 26, 2018 06:57
柯里化
function curry(fn, arity = fn.length) {
return (function nextCurried(prevArgs) {
return function curried(...nextArgs) {
var args = prevArgs.concat(nextArgs);
if (args.length >= arity) {
return fn(...args);
}
return nextCurried(args);
}
})([]);
@LeeeeeeM
LeeeeeeM / reduceAsMap.js
Created March 8, 2018 05:39
reduceAsMap
const double = x => x * x;
[1, 2, 3].map(double);
[1, 2, 3].reduce((result, item) => (
result.push(double(item)), result
), [])
@LeeeeeeM
LeeeeeeM / Class.js
Created March 10, 2018 02:57
javascript extends
var Class = (function () {
initial = false;
// var _mix = function(target, source) {
// for (var k in source) {
// if (!target.hasOwnProperty(k)) {
// target[k] = source[k];
// }
// }
// return target;
@LeeeeeeM
LeeeeeeM / selectionSort.js
Created March 10, 2018 03:55
选择排序
function selectionSort(array) {
var length = array.length;
for (var i = 0; i < length - 1; i++) {
var minIndex = i;
for(var j = i; j < array.length; j++) {
if (array[minIndex] > array[j]) {
minIndex = j;
}
}
if (minIndex !== i) {