天天看點

Rxjs Observable.pipe 傳入多個 operators 的執行邏輯分析

測試代碼:

fromEvent(this.test, 'click').pipe(map( event => event.timeStamp), mapTo(1)).subscribe((event) => console.log(event));
      

pipe 操作的兩個輸入操作:

Rxjs Observable.pipe 傳入多個 operators 的執行邏輯分析
<html>
<script>

var a = [1,2,3,4];

function fn(pre, cur, index, arr){
    console.log(`previous: ${pre}, cur: ${cur},
        index: ${index}, whole arr: ${arr}`);
    return pre + cur;
}

console.log(a.reduce( fn, 0));

</script>
</html>
      

reduce 接受兩個參數,第一個參數是一個函數,該函數接收 4 個輸入參數,previous,current,index 和 array:

previous:前一輪 reduce 疊代值,如果第一輪,則該值為第二個參數,即初始值

current:目前這輪的 reduce 疊代值。例如,array 元素為1,2,3,4,則 reduce 每輪疊代,current 值分别為1,2,3,4

index:疊代的索引值

array:調用 reduce 的原始數組,配合前一個 index 參數,能通路整個數組的内容

Rxjs Observable.pipe 傳入多個 operators 的執行邏輯分析
Rxjs Observable.pipe 傳入多個 operators 的執行邏輯分析
Rxjs Observable.pipe 傳入多個 operators 的執行邏輯分析

繼續閱讀