This file contains 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 throttle(fn, delay, context) { | |
var timer; | |
var context = context || this; | |
var first = true; | |
return function() { | |
if (first) { | |
fn.apply(context, [].slice.apply(arguments)); | |
first = false; | |
} | |
if (timer) { |
This file contains 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 throttle(fn, timeout, mustRunTime, context) { | |
var last; | |
var timer; | |
return function() { | |
current = Date.now(); | |
if (!last) { | |
last = current; | |
} | |
clearTimeout(timer); | |
if (current - last >= mustRunTime) { |
This file contains 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 insertSort(array) { | |
var length = array.length; | |
var j,temp; | |
for(var i = 0; i < length; i++) { | |
j = i; | |
temp = array[i]; | |
while(j > 0 && array[j - 1] > temp) { | |
array[j] = array[j - 1]; | |
j--; | |
} |
This file contains 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 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) { |
This file contains 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 bubbleSort(array) { | |
var length = array.length; | |
for (var i = 0; i < length - 1; i++) { | |
for (var j = 0; j< length - 1 -i; j++) { | |
if (array[j] > array[j + 1]) { | |
swap(array, j, j+1); | |
} | |
} | |
} |
This file contains 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 Class = (function () { | |
initial = false; | |
// var _mix = function(target, source) { | |
// for (var k in source) { | |
// if (!target.hasOwnProperty(k)) { | |
// target[k] = source[k]; | |
// } | |
// } | |
// return target; |
This file contains 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 double = x => x * x; | |
[1, 2, 3].map(double); | |
[1, 2, 3].reduce((result, item) => ( | |
result.push(double(item)), result | |
), []) |
This file contains 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 timeout = (time, value) => { | |
console.log('step into promise', time, value); | |
return new Promise(resolve => { | |
setTimeout(resolve, time, value); | |
}); | |
}; | |
var queue = [2, 3, 5, 99, 1]; | |
var dosth = value => { |
This file contains 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 compose(...fns) { | |
return function composed(result) { | |
var list = fns.slice(); | |
while(list.length > 0) { | |
result = list.pop()(result); | |
} | |
return result; | |
} | |
} |
This file contains 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 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); | |
} | |
})([]); |