js
compose
ES5
1 | function compose() { |
ES6
1 | const compose = (...args) => x => args.reduceRight((a, b) => b(a), x); |
pipe
ES5
1 | function pipe() { |
ES6
1 | const pipe = (...args) => x => args.reduce((a, b) => b(a), x); |
flat
ES5
1 | function flat(source, initial) { |
ES6
1 | const flat = (source, inital = []) => source.reduce((a, b) => Array.isArray(b) ? flat(b, a) : a.concat(b), inital); |
layer Flat
ES5
1 | function flat(source, layer, initial) { |
ES6
1 | const flat = (source, layer = 1, initial = []) => source.reduce((a, b) => (Array.isArray(b) && layer > 1) ? flat(b, layer - 1, a) : a.concat(b), initial); |
fibonacci
ES5
1 | function fibonacci(n) { |
ES6
1 | const fibonacci = n => (n === 1 || n === 0) ? n : (fibonacci(n - 1) + fibonacci(n - 2)); |
tailCall Fibonacci
ES5
1 | function fibonacci(n, n1, n2) { |
ES6
1 | const fibonacci = (n, n1 = 0, n2 = 1) => (n === 0) ? n1 : fibonacci(n - 1, n2, n1 + n2); |
cache Fibonacci
ES5
1 | function memo(fn, hasher) { |
ES6
1 | const memo = (fn, hasher) => { |
curry
ES5
1 | function curry(fn) { |
ES6
1 | const curry = (fn) => { |
debounce
ES5
1 | function debounce(fn, timeout) { |
ES6
1 | const debounce = (fn, timeout) => { |
throttle
ES5
1 | function throttle(fn, timeout) { |
ES6
1 | const throttle = (fn, timeout) => { |
factorial
ES5
1 | function factorial(n) { |
ES6
1 | const factorial = n => (n === 1) ? n : n * factorial(n - 1); |
tailCall Factorial
ES5
1 | function factorial(n, p) { |
ES6
1 | const factorial = (n, p = 1) => n === 1 ? p : factorial(n - 1, p * n); |
shallow Copy
ES5
1 | function shallowCopy(o) { |
ES6
1 | const shallowCopy = o => { |
deep Clone
ES5
1 | function deepClone(o) { |
ES6
1 | const deepClone = o => { |
deep Clone Loop
ES5
1 | function deepClone(o) { |
ES6
1 | const deepClone = o => { |
pick
ES5
1 | function pick(o, property) { |
ES6
1 | const pick = (o, property) => { |
new
ES5
1 | function myNew(fn) { |
ES6
1 | const myNew = (fn, ...args) => { |
instanceof
ES5
1 | function myInstanceof(targetObj, targetClass) { |
ES6
1 | const myInstanceof = (targetObj, targetClass) => { |
unit
ES5
1 | function unit(desc, fn) { |
ES6
1 | const unit = (desc, fn) => { |
call
ES5
1 | Function.prototype.myCall = function () { |
ES6
1 | Function.prototype.myCall = function (o = window, ...args) { |
apply
ES5
1 | Function.prototype.myApply = function () { |
ES6
1 | Function.prototype.myApply = function (o = window, args = []) { |
bind
ES5
1 | Function.prototype.myBind = function (context) { |
ES6
1 | Function.prototype.myBind = function (context, ...args) { |
softBind
ES5
1 | Function.prototype.softBind = function (context) { |
ES6
1 | Function.prototype.softBind = function (context, ...args) { |
Event Loop
browser
浏览器中的 Event Loop 其实比较简单,按照流程的步骤如下:
- 首先 JS 线程会区分异/同步程序,遇到同步程序优先执行,遇到异步程序会将其移入至相关的处理线程(定时器线程、异步 HTTP
请求线程) - 接着,会将相关的处理线程符合条件的回调事件移入至事件触发线程内,进入 Event Loop 事件循环执行机制
- Event Loop 会等待至同步程序全部执行完成后,轮询事件队列中的回调事件,触发执行
- Event Loop 中的事件队列分为两种: 宏事件、微事件,在每次触发宏事件之前,都会先去轮询执行微事件
- 宏事件
- script
- I/O
- setTimeout、setInterval
- setImmediate(Node.js)
- UI 事件
- postMessage
- 微事件
- Promise
- process.nextTick(Node.js)
- Object.observe
- MutationObserver
- 宏事件
nodejs
nodejs 中的 Event Loop 就有所不同, 其运行的阶段分为:
- timer: 执行 setTimeout、setInterval 回调事件
- pending callbacks: 执行延迟到下一个 I/O 回调
- idle,prepare: 仅系统内部使用
- poll: 检索新的 I/O 事件,执行与 I/O 相关的回调
- check: 执行 setImmediate 回调
- close callbacks: 执行一些关闭的回调函数,如 socket.close
注意: poll 队列执行完成后,如果没有 setImmediate,但是有定时器到期,会绕回执行定时器 timer 阶段
Promise
ES5
1 | var Promise = (function () { |
ES6
1 | const Promise = (() => { |
complete Promise
ES5
1 | var Promise = (function () { |
ES6
1 | const Promise = (() => { |
complete Promise Test
ES5
1 | var Promise = (function () { |
ES6
1 | const Promise = (() => { |
generator Thunk
ES5
1 | function Thunk(fn) { |
ES6
1 | const Thunk = fn => (...args) => callback => fn.call(this, ...args, callback); |
promise Generator Thunk
ES5
1 | function Thunk(fn) { |
ES6
1 | const Thunk = fn => (...args) => callback => fn(...args, callback); |
promise Timeout
ES5
1 | function timeoutPromise(promise, timeout) { |
ES6
1 | const timerPromise = (promise, timeout) => Promise.race([Promise.resolve(promise).then(value => |
get Character’s Length
ES5
1 | function getLength(str) { |
ES6
1 | const getLength = str => (str.toString() || String(str)).match(/[\s\S]/ug).length; |
judge UTF-16 Character
ES5
1 | function isUTF16(str) { |
ES6
1 | const isUTF16 = str => (str.toString() || String(str)).codePointAt(0) > 0xFFFF; |
flags Method Imitate
ES5
1 | function flagsImitate(regExp) { |
ES6
1 | const flagsImitate = (regExp) => { |
class imitate
ES5
1 | let classImitate = (function () { |
ES6
1 | let classImitate = (() => { |
class Rename Imitate
ES5
1 | let classImitate = (function () { |
ES6
1 | let classImitate = (() => { |
class with array attributes
ES5
1 | function toUnit32(key) { |
ES6
1 | class MyArray extends Array { |
redux imitate
ES5
1 | function createStore(state, stateChanger) { |
ES6
1 | const createStore = (state, stateChanger) => { |
redux complete imitate
ES5
1 | function createStore(reducer) { |
ES6
1 | const createStore = reducer => { |
isPlainObject
ES5
1 | function isObject(o) { |
ES6
1 | const isObject = o => { |
redux isPlainObject
ES5
1 | function isPlainObject(o) { |
ES6
1 | const isPlainObject = o => { |
redux createStore sourceCode
ES5
1 | const Action_Types = { |
ES6
1 | const Action_Types = { |
dateFormat ago
ES5
1 | Date.prototype.__defineGetter__('ago', function () { |
ES6
1 | Date.prototype.__defineGetter__('ago', function () { |
resolvePath CLI
ES5
1 | var fs = require('fs'); |
ES6
1 | const fs = require('fs'); |
tcp chat
ES5
1 | var net = require('net'); |
ES6
1 | const net = require('net'); |