源代码:
<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的原创文章,尽在:"汪子熙":
网友评论