exportconst sleep = ms =>newPromise(resolve => setTimeout(resolve, ms))
isStatic
检测数据是不是除了 symbol 外的原始数据
1 2 3 4 5 6 7 8 9 10
functionisStatic(value) { const type = typeof value return ( type === 'string' || type === 'number' || type === 'boolean' || type === 'undefined' || value === null ) }
isPrimitive
检测数据是不是原始数据
1 2 3
functionisPrimitive(value) { return isStatic(value) || typeof value === 'symbol' }
isObject
判断数据是不是引用类型的数据 (例如: Array, Function, Object, Regexp, new Number(0),以及 new String(‘’))
1 2 3 4
functionisObject(value) { const type = typeof value return value !== null && (type === 'object' || type === 'function') }
isObjectLike
检查 value 是否是 类对象。 如果一个值是类对象,那么它不应该是 null,而且 typeof 后的结果是 “object”
1 2 3
functionisObjectLike(value) { return value !== null && typeof value === 'object' }
pitch 方法在 Loader 中便是从左到右执行的,并且可以通过 data 这个变量来进行 pitch 和 normal 之间传递。熔断功能
正常顺序:
1 2 3 4 5 6 7
|- a-loader `pitch` |- b-loader `pitch` |- c-loader `pitch` |- requested module is picked up as a dependency |- c-loader normal execution |- b-loader normal execution |- a-loader normal execution
exports.module = { rules: [ { test: /.js$/, // 1) replace your original list of loaders with "happypack/loader": // loaders: [ 'babel-loader?presets[]=es2015' ], use: 'happypack/loader', include: [ /* ... */ ], exclude: [ /* ... */ ] } ] }
exports.plugins = [ // 2) create the plugin: new HappyPack({ // 3) re-add the loaders you replaced above in #1: loaders: ['babel-loader?presets[]=es2015'] }) ]
Tree-Shaking
webpack 自带 Tree-Shaking, scope hosting
1 2 3 4 5 6 7 8
// scope hosting 作用域提升,去除无用代码 const bar = 1 const foo = 2 const foobar = bar + foo console.log(foobar)
if (!this.head) { this.head = node } else { let current = this.head while (current.next) { current = current.next } current.next = node }
this.length += 1 }
insert(position, data) { if (position < 0 || position > this.length) { returnfalse }
const node = new Node(data) if (position === 0) { node.next = this.head this.head = node } else { let current = this.head let prev = null let index = 0 while (index++ < position) { prev = current current = current.next } node.next = current prev.next = node } this.length += 1 returntrue }
get(position) { if (position < 0 || position >= this.length) returnnull let index = 0 let current = this.head while (index++ < position) { current = current.next } return current.data }
indexOf(data) { let current = this.head let index = 0
while (current) { if (current.data === data) { return index } current = current.next index += 1 }
return-1 }
update(position, data) { if (position < 0 || position >= this.length) returnfalse let index = 0 let current = this.head while (index++ < position) { current = current.next } current.data = data returntrue }
removeAt(position) { if (position < 0 || position >= this.length) { returnfalse }
if (position === 0) { this.head = this.head.next } else { let index = 0 let current = this.head let prev = null while (index++ < position) { prev = current current = current.next }
prev.next = current.next } this.length -= 1
returntrue }
remove(data) { const position = this.indexOf(data) returnthis.removeAt(position) }
size() { returnthis.length }
isEmpty() { return !this.length }
toString() { let current = this.head let str = '' while (current) { str += current.toString() + ',' current = current.next } return str } }
let current = this.head let index = 0 while (index++ < position) { current = current.next } node.next = current node.prev = current.prev current.prev.next = node current.prev = node this.length += 1 returntrue }
getNode(position) { if (position < 0 || position >= this.length) returnnull
const flag = this.length / 2 > position if (flag) { let index = 0 let current = this.head while (index++ < position) { current = current.next } return current } else { let index = this.length let current = this.tail while (index-- < position) { current = current.next } return current } }