摘要:An interesting application of iterators is to not iterate over a fixed collection. For instance an iterator can be used to generate sequential members of an infinite set like the fibonacci sequence:

脚本宝典收集整理的这篇文章主要介绍了

js实例教程-js设计模式之BehavioralPatterns之Iterator

脚本宝典小编觉得挺不错的,现在分享给大家,也给大家做个参考,希望能帮助你少写一行代码,多一份安全和惬意。

小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。

 class KingSuccession {     constructor(inLineForThrone){         this.inLineForThrone = inLineForThrone;         this.pointer = 0;     }     next(){         return this.inLineForThrone[this.pointer++];     } } var kin = new KingSuccession(["Robert Baratheon","JofferyBaratheon","TommenBaratheon"]); kin.next(); kin.next(); kin.next();

An interesting application of iterators is to not iterate over a fixed collection. For
instance an iterator can be used to generate sequential members of an infinite set
like the fibonacci sequence:

 class FibonacciIterator{     constructor(){         this.previous = 1;         this.beforePrevious = 1;     }     next(){         var current = this.previous + this.beforePrevious;         this.beforePrevious = this.previous;         this.previous = current;         return current;     } } var fib = new FibonacciIterator(); fib.next(); fib.next(); fib.next();

Iterators are a syntactic nicety that has long been missing from JavaScript. Another
great feature of ECMAScript-2015 are generators. This is, in effect, a built in iterator
factory. Our fibonacci sequence could be rewritten like the following:

 function* FibonacciGenerator () {     var previous = 1;     var beforePrevious = 1;     while (true){         var current = previous + beforePrevious;         beforePrevious = previous;         previous = current;         yield  current;     } } var fibo = new FibonacciGenerator(); fibo.next().value; fibo.next().value; fibo.next().value;

 class KingSuccession {     constructor(inLineForThrone){         this.inLineForThrone = inLineForThrone;         this.pointer = 0;     }     next(){         return this.inLineForThrone[this.pointer++];     } } var kin = new KingSuccession(["Robert Baratheon","JofferyBaratheon","TommenBaratheon"]); kin.next(); kin.next(); kin.next();

An interesting application of iterators is to not iterate over a fixed collection. For
instance an iterator can be used to generate sequential members of an infinite set
like the fibonacci sequence:

 class FibonacciIterator{     constructor(){         this.previous = 1;         this.beforePrevious = 1;     }     next(){         var current = this.previous + this.beforePrevious;         this.beforePrevious = this.previous;         this.previous = current;         return current;     } } var fib = new FibonacciIterator(); fib.next(); fib.next(); fib.next();

Iterators are a syntactic nicety that has long been missing from JavaScript. Another
great feature of ECMAScript-2015 are generators. This is, in effect, a built in iterator
factory. Our fibonacci sequence could be rewritten like the following:

 function* FibonacciGenerator () {     var previous = 1;     var beforePrevious = 1;     while (true){         var current = previous + beforePrevious;         beforePrevious = previous;         previous = current;         yield  current;     } } var fibo = new FibonacciGenerator(); fibo.next().value; fibo.next().value; fibo.next().value;

觉得可用,就经常来吧!Javascript技巧 脚本宝典 欢迎评论哦! js技巧,巧夺天工,精雕玉琢。小宝典献丑了!

总结

以上是脚本宝典为你收集整理的

js实例教程-js设计模式之BehavioralPatterns之Iterator

全部内容,希望文章能够帮你解决

js实例教程-js设计模式之BehavioralPatterns之Iterator

所遇到的程序开发问题,欢迎加入QQ群277859234一起讨论学习。如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典网站推荐给程序员好友。 本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。

80%的人都看过