zoukankan      html  css  js  c++  java
  • [RxJS] Reactive Programming

    RxJS is super when dealing with the dynamic value. 

    Let's see an example which not using RxJS:

    var a = 4;
    var b = a * 10;
    
    console.log(b); // 40
    
    a = 5;
    console.log(b); // 40

    So you change a, it won't affect b's value because b is already defined....

    So if you want b get sideeffect, you need to declear it again:

    var a = 4;
    var b = a * 10;
    
    console.log(b); // 40
    
    a = 5;
    b = a * 12;
    console.log(b); // 60

     And also, many a time, it may happened that you found the variable has been mutated, but you have no idea where and how.

    So using RxJS:

    var streamA = Rx.Observable.interval(400).take(5);
    var streamB = streamA.map( (num) => {
      return num * 10;
    })
    
    streamB.subscribe(b => console.log(b));

    SO now, streamA arrivals at every 400ms, we can take 4 of those. then we map to StreamB.

    Then in the console, you will see the value will change according to the value of stream A.

  • 相关阅读:
    HDU 1010 Tempter of the Bone
    HDU 4421 Bit Magic(奇葩式解法)
    HDU 2614 Beat 深搜DFS
    HDU 1495 非常可乐 BFS 搜索
    Road to Cinema
    Sea Battle
    Interview with Oleg
    Spotlights
    Substring
    Dominating Patterns
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5252572.html
Copyright © 2011-2022 走看看