<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script language="JavaScript" type="text/javascript">
function range_(min,max){
return {
min:function(){ return min;},
max:function(){ return max; },
_iterator_:function() {
var val=Math.ceil(min);
return {
next:function(){
if(val>max) throw StopIteration;
return val++;
}
}
}
}
}
var r=new range_(160,180);
var s=r._iterator_();
console.log(r.min());
console.log(r.max);
console.log(r instanceof Object);
while(true){
try{
console.log(s.next());//写成:console.log(r._iterator_().next());就运行错误了
}catch(e){
if (e==StopIteration) break;
else throw e;
}
}
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script language="JavaScript" type="text/javascript">
function range_(min,max){
return {
min:function(){ return min;},
max:function(){ return max; },
_iterator_:function() {
var val=Math.ceil(min);
return {
next:function(){
if(val>max) throw StopIteration;
return val++;
}
}
}
}
}
var r=new range_(160,180);
var s=r._iterator_();
console.log(r.min());
console.log(r.max);
console.log(r instanceof Object);
while(true){
try{
console.log(s.next());//写成:console.log(r._iterator_().next());就运行错误了
}catch(e){
if (e==StopIteration) break;
else throw e;
}
}
运行结果:
注意
console.log(r.min());
console.log(r.max);
的差别