首页 > Web开发 > 详细

[RxJS] Reactive Programming - Why choose RxJS?

时间:2016-03-08 02:09:42      阅读:130      评论:0      收藏:0      [点我收藏+]

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.

[RxJS] Reactive Programming - Why choose RxJS?

原文:http://www.cnblogs.com/Answer1215/p/5252572.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!