关于......和对象的问题 [英] question about for...in and objects

查看:89
本文介绍了关于......和对象的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,我添加了一个名为contains的方法。我希望
想要使用for ... in ...我理解

完全是因为... in真的是对象而不是数组,但是我/ b $ b故意让这个数组填充不正常,因为

数组的键是作为一个ID在我的
$ b中具有上下文含义$ b脚本。

问题当然是因为... in也返回我的方法

''包含'作为键之一。有没有办法解决这个问题?

解决方案

ablock写道:


我有一个数组,我添加了一个名为contains的方法。我希望
想要使用for ... in ...我理解

完全是因为... in真的是对象而不是数组,但是我/ b $ b故意让这个数组填充不正常,因为

数组的键是作为一个ID在我的
$ b中具有上下文含义$ b脚本。

问题当然是因为... in也返回我的方法

''包含'作为键之一。有没有解决的办法?



您可以检查循环中找到的每个项目的类型。例如,如果

(typeof myarray [i] ==" function")那么那就是你的包含函数。


ablock写道:


我有一个数组,我添加了一个名为contains的方法。我希望
想要使用for ... in ...我理解

完全是因为... in真的是对象而不是数组,



不,不幸的是你根本不懂。数组是对象 -

数组对象,有Array()作为它们的构造函数。


但我故意填充这个数组,因为key
数组的
意味着在我的脚本中充当一个具有上下文含义的ID。



这是不可能的,因为密钥数组数据结构的数据由Array对象封装的
是整数(元素的索引),

和ID值不允许是基于SGML的标记中的整数语言

(比如HTML)。


可能你已经为你的数组

对象添加了一些非数字名称的属性,您可以使用任何本机ECMAScript对象(CMIIW),因为

它们都从Object.prototype继承。在这种情况下,您应该使用

Object对象而不是Array对象,除非您尝试在ECMAScript实现中实现PHP

关联数组。


问题当然是,对于... in也会返回我的方法

''包含''作为其中一个键。有没有解决的办法?



不在当前客户端ECMAScript实现中,这可能是你想要的

。用户定义的属性不能在那里给出

DontEnum属性。

PointedEars

-

现实主义:HTML 4.01严格

福音:XHTML 1.0严格

疯狂:XHTML 1.1作为应用程序/ xhtml + xml

- Bjoern Hoehrmann


ablock说:


>
我有一个数组,我添加了一个方法叫包含。我希望横向使用这个阵列... ... ...我完全理解
对于...来说它真的是用于对象而不是数组,但是我故意有因为
数组的键用作在我的
脚本中具有上下文含义的ID,所以此数组的顺序非常重要。



那么为什么要使用数组呢?使用一个对象。


>问题当然是,对于... in也会返回我的方法
''contains''作为一个的钥匙。有没有解决的办法?



是的。如果索引==包含,则忽略它。

如果您要添加更多方法,请检查

typeof index的值
或者,如果您的有效指数是数字,则忽略

isNaN(指数)为真的指数。

-


I have an array to which i have a added a method called contains. I
would like to transverse this array using for...in...I understand
fully that for...in is really meant for Objects and not Arrays, but I
purposely had this array filled unsequentially because the key for the
array is meant to act as an ID which has a contextual meaning in my
script.
The problem, of course, is that for...in also returns my method
''contains'' as one of the keys. Is there a way around this?

解决方案

ablock wrote:

I have an array to which i have a added a method called contains. I
would like to transverse this array using for...in...I understand
fully that for...in is really meant for Objects and not Arrays, but I
purposely had this array filled unsequentially because the key for the
array is meant to act as an ID which has a contextual meaning in my
script.
The problem, of course, is that for...in also returns my method
''contains'' as one of the keys. Is there a way around this?

You could check the typeof each item found in the loop. For example if
(typeof myarray[i]=="function") then that''s your contains function.


ablock wrote:

I have an array to which i have a added a method called contains. I
would like to transverse this array using for...in...I understand
fully that for...in is really meant for Objects and not Arrays,

No, unfortunately you don''t understand at all. Arrays are objects --
Array objects, having Array() as their constructor.

but I purposely had this array filled unsequentially because the key
for the array is meant to act as an ID which has a contextual meaning
in my script.

That would be unlikely, since the "keys" of an array data structure
encapsuled by an Array object are integers (the indexes of the elements),
and ID values are not allowed to be integers in SGML-based markup languages
(like HTML).

Probably you have added some properties with non-numeric name to your Array
object, which you could have with any native ECMAScript object (CMIIW), as
they all inherit from Object.prototype. In that case, you should use an
Object object instead of an Array object, unless you try implementing PHP
associative arrays in an ECMAScript implementation.

The problem, of course, is that for...in also returns my method
''contains'' as one of the keys. Is there a way around this?

Not in current client-side ECMAScript implementations, and that is probably
what you are looking for. User-defined properties can''t be given the
DontEnum attribute there.
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann


ablock said:

>
I have an array to which i have a added a method called contains. I
would like to transverse this array using for...in...I understand
fully that for...in is really meant for Objects and not Arrays, but I
purposely had this array filled unsequentially because the key for the
array is meant to act as an ID which has a contextual meaning in my
script.

So why use an Array at all? Use an Object.

>The problem, of course, is that for...in also returns my method
''contains'' as one of the keys. Is there a way around this?

Yes. Simply ignore it if the index=="contains".
If you''re going to be adding more methods, check the value of
typeof index
or, if your valid indices are numbers, ignore indices for which
isNaN(index) is true.
--


这篇关于关于......和对象的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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