javascript 原型继承大问题!

小范f-li.cn 2009-03-04 03:37:39
比如 我给Array 原型 上 加了个 show 方法
然后 在 for(var b in array) 中 总能访问到 我添加的 show成员
像 join 这样的方法 不会显示。。。
怎么办 才 能 不 让 for in 访问到 ?????
...全文
112 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
topscience 2009-03-06
  • 打赏
  • 举报
回复
修改不了上面的帖子,不过重点是这句: because the for...in statement iterates over user-defined properties in addition to the array elements

topscience 2009-03-06
  • 打赏
  • 举报
回复

兄弟,这是做不到的。IE我不知道,不过看看FF是怎么说的吧:

http://topic.csdn.net/u/20090304/15/4b55954a-9291-43b6-8fd2-5b3e2e10045d.html

Arrays
Although it may be tempting to use this as a way to iterate over Array elements, because the for...in statement iterates over user-defined properties in addition to the array elements, if you modify the Array object, such as adding custom properties or methods, the for...in statement will return the name of your user-defined properties in addition to the numeric indexes. Thus it is better to use a traditional for loop with a numeric index when iterating over arrays.
cloudgamer 2009-03-05
  • 打赏
  • 举报
回复
这就叫那个什么污染
这是没办法的事情
dyydingding 2009-03-05
  • 打赏
  • 举报
回复
up
小范f-li.cn 2009-03-05
  • 打赏
  • 举报
回复
up
小范f-li.cn 2009-03-04
  • 打赏
  • 举报
回复
比如 array 的 join 等 js 原有的 成员 就 for in 不出来啊!,但 alert("join" in new Array()); 为true.
我就想他 for in 不出来。。。。。。!!!!!
lxy0502030314 2009-03-04
  • 打赏
  • 举报
回复
你给原型添加了肯定会访问到啊,for in语句本来就是遍历对象的所有属性

87,922

社区成员

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

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