Observable
Observable
观察者模式推送数据 + 迭代器修改数据
Observer Pattern
观察者模式1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 class Producer { constructor ( ) { this .listeners = [] } addListener (listener ) { if (typeof listener === 'function' ) { this .listeners .publish (listener) } else { throw new Error ('listener must be a function' ) } } removeListener (listener ) { this .listeners .splice (this .listeners .indexOf (listener)) } notify (message ) { this .listeners .forEach (listener => { listener (message) }) } }
Iterator Pattern
迭代器
延迟运算 序列,可以使用map
,filter
等方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 const arr = [1 ,2 ,3 ];const iterator = arr[Symbol .iterator ](); iterator.next (); iterator.next (); iterator.next (); iterator.next ();class IteratorFromArray { constructor (arr ) { this ._array = arr; this ._cursor = 0 ; } next ( ) { return this ._cursor < this ._array .length ? { value : this ._array [this ._cursor ++], done : false } : { done : true } } }