Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
向javascript类添加自定义迭代器_Javascript_Iterator_For In Loop - Fatal编程技术网

向javascript类添加自定义迭代器

向javascript类添加自定义迭代器,javascript,iterator,for-in-loop,Javascript,Iterator,For In Loop,我试图弄清楚如何将一个itertor添加到一个javascript类中,以便该类可以在for…in循环中使用。按照Mozilla的说明不会产生他们声称会产生的结果。 Jsfiddle给定示例: 功能范围(低、高){ 这个.低=低; 这个高=高; 该电流=低; this.next=函数(){ 如果(this.current>this.range.high) 抛出停止迭代; 其他的 返回这个.current++; } } 函数范围迭代器(范围){ 这个范围=范围; this.current=this

我试图弄清楚如何将一个itertor添加到一个javascript类中,以便该类可以在for…in循环中使用。按照Mozilla的说明不会产生他们声称会产生的结果。 Jsfiddle给定示例:

功能范围(低、高){
这个.低=低;
这个高=高;
该电流=低;
this.next=函数(){
如果(this.current>this.range.high)
抛出停止迭代;
其他的
返回这个.current++;
}
}
函数范围迭代器(范围){
这个范围=范围;
this.current=this.range.low;
}
RangeIterator.prototype.next=函数(){
如果(this.current>this.range.high)
抛出停止迭代;
其他的
返回这个.current++;
};
Range.prototype.\uuuu迭代器\uuuu=function(){
返回新的RangeIterator(此);
};
var范围=新范围(3,5);
用于(范围内的var i)
document.getElementById(“test”).innerHTML=i+“
”;//依次打印3、4、5
它不打印范围内的数字,而是打印“\uuuu迭代器”


有人知道如何实现这一点吗?

Mozilla文档指出,在JavaScript 1.7中引入了迭代器功能。尽管Chrome支持1.7中的一些功能,但它并没有得到完全支持,所以这不起作用。如果你在最新的Firefox版本中测试你的代码,你会发现它是有效的

尽管您可能希望附加范围值,而不是替换整个div

功能范围(低、高){
这个.低=低;
这个高=高;
该电流=低;
this.next=函数(){
如果(this.current>this.range.high)
抛出停止迭代;
其他的
返回这个.current++;
}
}
函数范围迭代器(范围){
这个范围=范围;
this.current=this.range.low;
}
RangeIterator.prototype.next=函数(){
如果(this.current>this.range.high)
抛出停止迭代;
其他的
返回这个.current++;
};
Range.prototype.\uuuu迭代器\uuuu=function(){
返回新的RangeIterator(此);
};
var范围=新范围(3,5);
用于(范围内的var i)
document.getElementById(“test”).innerHTML+=i+“
”;//依次打印3、4、5
使用ES2015,其简单:

function Range(start, end) {
    var ret = {};
    ret[Symbol.iterator] = function *() {
        while (start < end) 
            yield start++;
    }
    return ret;
}

使用ES2015,它可以变得更加简单

const Range=(开始、结束)=>({
*[符号.迭代器](){
while(开始<结束)
收益率开始++;
}
})
对于(范围(1,10)的var x){
console.log(x)

}
或者不使用生成器语法也可以执行此操作。如果您试图理解迭代器是如何工作的,这可能会有所帮助

功能范围(开始、结束){
返回{
[符号.迭代器](){
归还这个;
},
下一个(){

返回start,但是有没有不使用生成器语法的方法呢?
function Range(low, high){
  this.low = low;
  this.high = high;
  this.current = low;
  this.next = function() {
  if (this.current > this.range.high)
    throw StopIteration;
  else
    return this.current++;
  }
}
function RangeIterator(range){
  this.range = range;
  this.current = this.range.low;
}
RangeIterator.prototype.next = function(){
  if (this.current > this.range.high)
    throw StopIteration;
  else
    return this.current++;
};
Range.prototype.__iterator__ = function(){
  return new RangeIterator(this);
};
var range = new Range(3, 5);
for (var i in range)
  document.getElementById("test").innerHTML += i+"</br>"; // prints 3, then 4, then 5 in sequence
function Range(start, end) {
    var ret = {};
    ret[Symbol.iterator] = function *() {
        while (start < end) 
            yield start++;
    }
    return ret;
}
for (var x of Range(1, 10))