|
|
|
Next: Multiple draggables to be dropped
|
| Author |
Message |
External

Since: Aug 10, 2007 Posts: 8
|
(Msg. 1) Posted: Fri Aug 10, 2007 10:54 am
Post subject: question about for...in and objects Archived from groups: comp>lang>javascript (more info?)
|
|
|
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? |
|
| Back to top |
|
 |  |
External

Since: Jun 09, 2004 Posts: 1556
|
(Msg. 2) Posted: Fri Aug 10, 2007 11:23 am
Post subject: Re: question about for...in and objects Archived from groups: per prev. post (more info?)
|
|
|
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.
-- |
|
| Back to top |
|
 |  |
External

Since: Aug 08, 2007 Posts: 10
|
(Msg. 3) Posted: Fri Aug 10, 2007 8:00 pm
Post subject: Re: question about for...in and objects Archived from groups: per prev. post (more info?)
|
|
|
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. |
|
| Back to top |
|
 |  |
|
Thomas 'PointedEars' Lahn
|
External

Since: Sep 05, 2004 Posts: 3405
|
(Msg. 4) Posted: Fri Aug 10, 2007 8:20 pm
Post subject: Re: question about for...in and objects Archived from groups: per prev. post (more info?)
|
|
|
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 |
|
| Back to top |
|
 |  |
External

Since: Aug 10, 2007 Posts: 8
|
(Msg. 5) Posted: Fri Aug 10, 2007 8:30 pm
Post subject: Re: question about for...in and objects Archived from groups: per prev. post (more info?)
|
|
|
> No, unfortunately you don't understand at all. Arrays are objects --
> Array objects, having Array() as their constructor.
>
Yes, i am aware of that...what i meant is that for...in was designed
for those objects that don't have an integer as their keys and
therefore are not 'associative'. I am fully aware that 'associative
arrays' in ECMAScript are actually objects. I do know how to program
object oriented ECMAScript.
> 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.
>
I have no idea what you're talking about...you are clearly taking a
big problem and making it a huge one.
I have an array (object) as follows: a = {'1' = 'something', '6' =
'something else'......
My keys represent id's that have a contextual meaning in my script. I
don't know why you think that that's unlikely, or why you think that I
meant id's in an SGML tag.
In my case they happen to represent id's from a database table.
Now it's true...I am using the Array object, when i could be using the
Object object or any other object for that matter. That's besides the
point. The point is that my object, let's call it hotdog for arguments
sake, has a method added to it, and i wish to transverse this through
for...in, so I repeat:
> > 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.
>
This is the answer I should have received at the beginning of this
whole thing, so I wouldn't have wasted my time reading through the
whole message.
Thank you |
|
| Back to top |
|
 |  |
External

Since: Jun 09, 2004 Posts: 1556
|
(Msg. 6) Posted: Fri Aug 10, 2007 8:30 pm
Post subject: Re: question about for...in and objects Archived from groups: per prev. post (more info?)
|
|
|
ablock said:
>This is the answer I should have received at the beginning of this
>whole thing, so I wouldn't have wasted my time reading through the
>whole message.
Do you suppose it's possible that the reason you got a lot of
answers that you didn't want was because you didn't ask the
right question to begin with?
Thanks for playing.
-- |
|
| Back to top |
|
 |  |
External

Since: Aug 10, 2007 Posts: 8
|
(Msg. 7) Posted: Fri Aug 10, 2007 8:33 pm
Post subject: Re: question about for...in and objects Archived from groups: per prev. post (more info?)
|
|
|
> So why use an Array at all? Use an Object.
Because that doesn't solve my problem...
> 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.
>
> --
I am aware of all these solutions but they don't "solve" the
problem...thanks anyway |
|
| Back to top |
|
 |  |
External

Since: Aug 10, 2007 Posts: 1
|
(Msg. 8) Posted: Fri Aug 10, 2007 8:52 pm
Post subject: Re: question about for...in and objects Archived from groups: per prev. post (more info?)
|
|
|
On Aug 10, 10:54 am, ablock <atbl....TakeThisOut@gmail.com> wrote:
> The problem, of course, is that for...in also returns my method
> 'contains' as one of the keys. Is there a way around this?
See http://yuiblog.com/blog/2006/09/26/for-in-intrigue/ for a
workaround. |
|
| Back to top |
|
 |  |
External

Since: Aug 10, 2007 Posts: 8
|
(Msg. 9) Posted: Fri Aug 10, 2007 9:16 pm
Post subject: Re: question about for...in and objects Archived from groups: per prev. post (more info?)
|
|
|
> Do you suppose it's possible that the reason you got a lot of
> answers that you didn't want was because you didn't ask the
> right question to begin with?
No i didn't realize it...considering my question was clear enough that
even you answered it...albeit not with an answer that helped my cause.
> Thanks for playing.
You lose. |
|
| Back to top |
|
 |  |
External

Since: Jun 09, 2004 Posts: 1556
|
(Msg. 10) Posted: Fri Aug 10, 2007 9:16 pm
Post subject: Re: question about for...in and objects Archived from groups: per prev. post (more info?)
|
|
|
ablock said:
>
>> Do you suppose it's possible that the reason you got a lot of
>> answers that you didn't want was because you didn't ask the
>> right question to begin with?
>
>No i didn't realize it...considering my question was clear enough that
>even you answered it...albeit not with an answer that helped my cause.
And you still don't realize that we were answering the question that
you asked. Just not the one that you *meant* to ask.
Your gratitude is going to ensure that many people will rush to help
you in the future.
-- |
|
| Back to top |
|
 |  |
External

Since: Aug 10, 2007 Posts: 8
|
(Msg. 11) Posted: Fri Aug 10, 2007 9:18 pm
Post subject: Re: question about for...in and objects Archived from groups: per prev. post (more info?)
|
|
|
On Aug 10, 4:52 pm, Justin McConnell <bool....DeleteThis@gmail.com> wrote:
> On Aug 10, 10:54 am, ablock <atbl....DeleteThis@gmail.com> wrote:
>
> > The problem, of course, is that for...in also returns my method
> > 'contains' as one of the keys. Is there a way around this?
>
> Seehttp://yuiblog.com/blog/2006/09/26/for-in-intrigue/for a
> workaround.
Thank you. |
|
| Back to top |
|
 |  |
|
Thomas 'PointedEars' Lahn
|
External

Since: Sep 05, 2004 Posts: 3405
|
(Msg. 12) Posted: Fri Aug 10, 2007 11:56 pm
Post subject: Re: question about for...in and objects Archived from groups: per prev. post (more info?)
|
|
|
ablock wrote:
>> No, unfortunately you don't understand at all. Arrays are objects --
>> Array objects, having Array() as their constructor.
>
> Yes, i am aware of that...what i meant is that for...in was designed
> for those objects that don't have an integer as their keys and
> therefore are not 'associative'. I am fully aware that 'associative
> arrays' in ECMAScript are actually objects. I do know how to program
> object oriented ECMAScript.
Apparently you don't.
>> 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.
>
> I have no idea what you're talking about...you are clearly taking a
> big problem and making it a huge one.
Obviously you have no clue what I am talking about because you are lacking
basic knowledge.
> I have an array (object) as follows: a = {'1' = 'something', '6' =
> 'something else'......
`a' is *not* assigned a reference to an Array object here.
It could be a reference to an Object object, initialized with an Object
literal, if the "inner" `=' would be replaced by `:'.
It could be a reference to an Array object, initialized with an Array
literal, if `{' and `}' were replaced by `[' and `]', respectively, and the
"inner" `=' were replaced by `,'.
Other than that, it is merely a big syntax error in ECMAScript
implementations, because strings are immutable and you are trying to assign
(`=') literally 'something' to a string (literal).
However, curly braces are part of the syntax for a Java array. So it may as
well be that you have asked in the wrong newsgroup. In that case, try one
of comp.lang.java.* instead. Or you have simply not understood that
JavaScript has nothing to do with Java but the first four characters.
Probably we'll never know.
Had you posted *some* code in the first place, this could have been
clarified right away.
> Now it's true...I am using the Array object,
No, you are not. So far, you are not using anything.
> when i could be using the Object object or any other object for that matter.
> That's besides the point.
This is not a support forum. I post on Usenet whatever comment I deem
necessary regarding your article. And I found it necessary to point out
that you waste memory and (probably) run time, one cause of your not
understanding the very *basics* of the language but immodestly claiming
otherwise.
> The point is that my object, let's call it hotdog for arguments
> sake, has a method added to it, and i wish to transverse this through
> for...in, so I repeat:
How boring.
>>> 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.
>
> This is the answer I should have received at the beginning of this
> whole thing, so I wouldn't have wasted my time reading through the
> whole message.
Probably not.
PointedEars |
|
| Back to top |
|
 |  |
External

Since: Aug 10, 2007 Posts: 8
|
(Msg. 13) Posted: Sun Aug 12, 2007 3:47 am
Post subject: Re: question about for...in and objects Archived from groups: per prev. post (more info?)
|
|
|
On Aug 10, 5:42 pm, Lee <REM0VElbspamt... RemoveThis @cox.net> wrote:
> ablock said:
>
>
>
> >> Do you suppose it's possible that the reason you got a lot of
> >> answers that you didn't want was because you didn't ask the
> >> right question to begin with?
>
> >No i didn't realize it...considering my question was clear enough that
> >even you answered it...albeit not with an answer that helped my cause.
>
> And you still don't realize that we were answering the question that
> you asked. Just not the one that you *meant* to ask.
>
> Your gratitude is going to ensure that many people will rush to help
> you in the future.
>
> --
Not sure what you're talking about...I never said anybody didn't
answer my question. I said that the answers given didn't *address* my
problem. In otherwords, yes, those solutions work, but they are
bandaids...they don't solve the *root* of the issue. Everybody here
answered my question, just not with an answer that I already didn't
know. So I don't know what you meant by "the question that [I] *meant*
to ask".
I'm also not sure what you were implying in your last statement since
I was careful to thank everybody who gave an answer...including you.
Thank you for your time. |
|
| Back to top |
|
 |  |
External

Since: Jun 09, 2004 Posts: 1556
|
(Msg. 14) Posted: Sun Aug 12, 2007 3:47 am
Post subject: Re: question about for...in and objects Archived from groups: per prev. post (more info?)
|
|
|
ablock said:
>
>On Aug 10, 5:42 pm, Lee <REM0VElbspamt... DeleteThis @cox.net> wrote:
>> ablock said:
>>
>>
>>
>> >> Do you suppose it's possible that the reason you got a lot of
>> >> answers that you didn't want was because you didn't ask the
>> >> right question to begin with?
>>
>> >No i didn't realize it...considering my question was clear enough that
>> >even you answered it...albeit not with an answer that helped my cause.
>>
>> And you still don't realize that we were answering the question that
>> you asked. Just not the one that you *meant* to ask.
>>
>> Your gratitude is going to ensure that many people will rush to help
>> you in the future.
>>
>> --
>
>Not sure what you're talking about...I never said anybody didn't
>answer my question. I said that the answers given didn't *address* my
>problem. In otherwords, yes, those solutions work, but they are
>bandaids...they don't solve the *root* of the issue. Everybody here
>answered my question, just not with an answer that I already didn't
>know. So I don't know what you meant by "the question that [I] *meant*
>to ask".
>
>I'm also not sure what you were implying in your last statement since
>I was careful to thank everybody who gave an answer...including you.
The point you're missing is that you asked for solutions, but
actually rejected any that didn't solve the *root* of the issue.
That's not the normal criteria for acceptable answers, so you
should have made it clear from the beginning.
"I'm aware of these solutions but they don't "solve" the problem,
thanks anyway", sounds more like sarcasm than gratitude.
At this point, I'll assume it's just more miscommunication.
-- |
|
| Back to top |
|
 |  |
External

Since: Aug 10, 2007 Posts: 8
|
(Msg. 15) Posted: Sun Aug 12, 2007 4:05 am
Post subject: Re: question about for...in and objects Archived from groups: per prev. post (more info?)
|
|
|
> >> No, unfortunately you don't understand at all. Arrays are objects --
> >> Array objects, having Array() as their constructor.
>
> > Yes, i am aware of that...what i meant is that for...in was designed
> > for those objects that don't have an integer as their keys and
> > therefore are not 'associative'. I am fully aware that 'associative
> > arrays' in ECMAScript are actually objects. I do know how to program
> > object oriented ECMAScript.
>
> Apparently you don't.
Why is that so apparent?
>
> >> 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.
>
> > I have no idea what you're talking about...you are clearly taking a
> > big problem and making it a huge one.
>
> Obviously you have no clue what I am talking about because you are lacking
> basic knowledge.
>
Like what? Please elaborate.
> > I have an array (object) as follows: a = {'1' = 'something', '6' =
> > 'something else'......
>
> `a' is *not* assigned a reference to an Array object here.
>
> It could be a reference to an Object object, initialized with an Object
> literal, if the "inner" `=' would be replaced by `:'.
>
> It could be a reference to an Array object, initialized with an Array
> literal, if `{' and `}' were replaced by `[' and `]', respectively, and the
> "inner" `=' were replaced by `,'.
>
> Other than that, it is merely a big syntax error in ECMAScript
> implementations, because strings are immutable and you are trying to assign
> (`=') literally 'something' to a string (literal).
>
This was not my syntax...I just demonstrating a point. Please stick to
the topic at hand...
Since you keep missing the point, maybe i'll repeat it again in
clearer language.
I don't care if it's you wanna call it an array, an object or a
hotdog. At this point its a semantic argument and has no relevance to
the discussion at hand. I just have a hotdog which is not filled
sequentially, so that indice '1' has a value, and '6' has a value,
etc. I just want to iterate through my hotdog but apparently I learned
that I can't do that so simply in ECMAScript.
I'm not sure if I can make this much clearer than this. Of course, I'm
not asking a question anymore so this all a mute point.
> However, curly braces are part of the syntax for a Java array. So it may as
> well be that you have asked in the wrong newsgroup. In that case, try one
> of comp.lang.java.* instead. Or you have simply not understood that
> JavaScript has nothing to do with Java but the first four characters.
> Probably we'll never know.
>
This is totally uncalled for. Learn some manners.
> Had you posted *some* code in the first place, this could have been
> clarified right away.
>
I asked a totally general question. There is no point in posting code.
Apparently all the other people who answered my question were able to
do so without me posting code.
> > Now it's true...I am using the Array object,
>
> No, you are not. So far, you are not using anything.
>
Sorry...I meant my hotdog...my bad...
> > when i could be using the Object object or any other object for that matter.
> > That's besides the point.
>
> This is not a support forum.
Therefore?
> necessary regarding your article. And I found it necessary to point out
> that you waste memory and (probably) run time, one cause of your not
What I waste in memory i gain back in speed by saving my self an extra
loop...
> >>> 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.
>
> > This is the answer I should have received at the beginning of this
> > whole thing, so I wouldn't have wasted my time reading through the
> > whole message.
>
> Probably not.
>
Actually...yes...this is the answer that I should have gotten in the
beginning...
When you answer questions, try to actually answer them instead of
trying to show off how nerdy you are like all the other fellows on
this forum. |
|
| Back to top |
|
 |  |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|
|
|