如何从"for..in"的处理中排除添加到Array方法中的方法.环形? (javascript) [英] How to exclude added to Array methods from processing in "for..in" loop? (javascript)

查看:56
本文介绍了如何从"for..in"的处理中排除添加到Array方法中的方法.环形? (javascript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Array中添加了一些有用的帮助器(例如Opera的toSource()).现在,for..in返回具有常规属性的函数.

I've added some useful helpers to Array (such as toSource() for Opera). And now for..in returns the functions with normal properties.

我现在正在使用for..in,因为使用此代码更容易阅读.而且它是js的本机功能,因此必须更快.

I'm using for..in now, because the code is easier to read with it. And it's a native functional of js, so must be faster.

但是在循环中添加类型检查使使用经典for(;;)更加容易.

But adding type checks in loop makes it easier to use classic for(;;).

有什么方法可以避免for..in枚举函数吗?

Is there any methods to avoid for..in enumerate functions ?

跨浏览器工作不是很必要(Opera中必须工作),但是速度很重要.

Cross-browser work is not very necessary (must work in Opera), but speed is important.

谢谢.

修改:
是否有能力避免从任何对象中枚举for..in枚举函数或自定义属性?


Is there any ability to avoid for..in enumerate functions or custom properties from any Object ?

推荐答案

您应该从不使用for..in循环来迭代数组元素. for..in专为遍历 properties 而设计,仅出于您刚刚描述的原因而应仅用于此目的.许多库会修改数组,日期等原型,因此在迭代数组元素时不应依赖for...使用for(;;)方法,可以保证做您想做的事情.而且它不比for..in循环快,因为它也是JavaScript的本机.

You should never use for..in loops to iterate over array elements. for..in is designed for iterating over properties, and should only be used for that, for exactly the reason you've just described. Many libraries modify array, date, etc prototypes, so you should not rely on for..in iterating just the array elements. Use the for(;;) method, it's guaranteed to do what you want. And it's no faster than a for..in loop, because it's native to javascript as well.

有关更多信息,请在 prototype.js 库中进行阅读.

For more info, read about it in the prototype.js library.

这篇关于如何从"for..in"的处理中排除添加到Array方法中的方法.环形? (javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆