官术网_书友最值得收藏!

Properties

Now you know what an EventStream is and how to create it; as discussed, it is an observable. However, bacon.js has another special form of observable called Property. A Property is an EventStream with the concept of current value. You can create a Property from any EventStream using the toProperty() or scan() method; it is especially useful to represent DOM data binding.

To create a Property from an EventStream, you can use the toProperty() method that has the following signature:

eventStream.toProperty(initialValue); 

The initialValue parameter is optional. If you decide to omit it, you will have a Property without an initial value. If you pass it, it will be used as the current value of this Property until the first value is emitted from the stream.

The other way to create a Property is using the scan() method. This method is similar to the reduce() method from the JavaScript array object. Given a seed object and accumulator function, the scan() method will iterate over your EventStream to create a Property from the result:

Bacon 
.sequentially(100,['a','b','c','d'])
.scan('=> ',(acc,b)=> acc+b)
.onValue((value)=>console.log(value));

In this example, we first create an EventStream from an array of strings, then we call the scan() method to create a Property with a seed value of => , and using a function to concatenate these value, we print the result to the console for each scan iteration. So running this code will give the following output:

    => 
=> a
=> ab
=> abc
=> abcd
主站蜘蛛池模板: 开化县| 岐山县| 宜宾县| 石棉县| 郴州市| 昔阳县| 滨州市| 邯郸县| 汉源县| 嘉义县| 杨浦区| 井研县| 准格尔旗| 克拉玛依市| 牡丹江市| 嵊州市| 左贡县| 商南县| 大荔县| 韶山市| 祁门县| 神池县| 达州市| 牟定县| 古蔺县| 宜川县| 太和县| 罗源县| 偃师市| 阿城市| 元谋县| 封丘县| 河曲县| 自贡市| 尉犁县| 丹江口市| 来安县| 沙坪坝区| 满城县| 榆树市| 吴川市|