Rico的prototype.js中类Enumerable的each方法应该怎么理解,懂的朋友来帮帮忙~~谢谢

lele0923 2006-03-28 03:56:52
var Enumerable = {
each: function(iterator) {
var index = 0;
try {
this._each(function(value) {
try {
iterator(value, index++);
} catch (e) {
if (e != $continue) throw e;
}
});
} catch (e) {
if (e != $break) throw e;
}
},
......

each 方法中的_each应该怎么理解?在哪里实现的,还有each方法完成了什么工作?
还有个问题:
Array.prototype._reverse = Array.prototype.reverse; 这句中的_reverse和reverse是什么关系有什么不同吗,都是什么意思呢?实在没找到资料可以查阅.懂的朋友帮帮忙~~~谢谢了!!
...全文
126 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ttyp 2006-03-28
  • 打赏
  • 举报
回复
_each是每个实现Enumerable接口都要实现的一个虚拟函数,这里只是先定义,具体实现由继承的子类来实现

var a = {
each:function(){this._each();}
}

var b = {
_each:function(){alert("hello");}
}

Object.extend(b,a);

b.each();

Array.prototype._reverse = Array.prototype.reverse;

这句是对reverse做了个备份,可能后面用的上
ffyd2000 2006-03-28
  • 打赏
  • 举报
回复
_reverse前面加_好像是说明是私有成员的样子
_reverse 和reverse应该是一样的,不过访问权限不同
ffyd2000 2006-03-28
  • 打赏
  • 举报
回复
我的理解:
_each的定义在后面
Array.prototype._reverse = Array.prototype.reverse;

Object.extend(Array.prototype, {
_each: function(iterator) {
for (var i = 0; i < this.length; i++)
iterator(this[i]);
}。。。

each的功能是对数组每个值执行iterator

个人看法

52,782

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧