rxjs里subscribeToArray的一个JavaScri

2021-02-08  本文已影响0人  华山令狐冲

源代码:

<html>
<script>

function subscribeToArray(array){
    return function(subscriber){
         for (var i = 0, len = array.length; i < len; i++) {
          subscriber.next(array[i]);
        }
      subscriber.complete();
    };

}

var input = [1,2,3];

var result = subscribeToArray(input);

var print = function(input){
    if(!!input){
        console.log('next: ' + input);
    }
    else{
        console.log('complete!');
    }
}

var oSubscriber = {
    next: print,
    complete: print
};

result(oSubscriber);
debugger;
</script>
</html>

输出:

在subscribeToArray返回的函数内部,输入array通过一个闭包被存储:

更多Jerry的原创文章,尽在:"汪子熙":


上一篇 下一篇

猜你喜欢

热点阅读