[Proto-Scripty] Re: Problem with prototype and arrays.

2010-08-03 Thread T.J. Crowder
Hi,

 I'm missing something here.

One of us is. :-)

The OP's problem was that using for..in on an array was looping
through the property names and he expected it to loop through array
indexes. That's the common misconception I was talking about. People
think for..in loops through array indexes, which is just plain wrong.
While it *does* that (because array indexes are property names), it's
not limited to doing that; it loops through all enumerable properties
(as you know). All will appear to be well in a vanilla environment
with a straightforward array instance, but not when either the
Array.prototype or the array instance has had other enumerable
properties added to it.

I think if you read the wiki article[1] I pointed him at (I wrote it,
a couple of years back) you'll see what I'm saying more clearly --
though since you clearly understand what for..in does, it may not be a
worthwhile way to spend your time. The article is for people like the
OP who aren't that familiar with JavaScript's arrays and the for..in
construct.

[1] http://proto-scripty.wikidot.com/prototype:tip-looping-through-arrays

-- T.J.

On Aug 3, 1:10 am, RobG rg...@iinet.net.au wrote:
 On Aug 3, 2:19 am, T.J. Crowder t...@crowdersoftware.com wrote:

  Hi,

Yes, it's an extremely common misconception.

   What, that for..in iterates over an object's properties?

  No, that it loops over array indexes.

 I'm missing something here. Array indexes are properties, which is why
 they are included in for..in iteration. The OPs problem was how to
 deal with properties from the [[prototype]] chain, not that array
 properties were included in for..in.

 There are two issues to deal with when using for..in with a javascript
 array:

  1. properties may have been added to Array.prototype, and

  2. object properties are not guaranteed to be returned in any
 particular order

 Both the above are common to any use of for..in. Item 1 is easily
 dealt with. If order matters, a for, while or do loop can be used.
 Otherwise, there is no issue.

 --
 Rob

-- 
You received this message because you are subscribed to the Google Groups 
Prototype  script.aculo.us group.
To post to this group, send email to prototype-scriptacul...@googlegroups.com.
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en.



[Proto-Scripty] Re: Problem with prototype and arrays.

2010-08-02 Thread RobG


On Aug 2, 3:43 pm, T.J. Crowder t...@crowdersoftware.com wrote:
 Hi,

  I found that array looping method online, I guess I will remove that
  page from my bookmarks.

 Yes, it's an extremely common misconception.

What, that for..in iterates over an object's properties? It's per
ECMA-262. There are strategies for dealing with properties on an
array's internal prototype chain so that for..in works just fine, even
when it's built-in prototype has been modified.


--
Rob

-- 
You received this message because you are subscribed to the Google Groups 
Prototype  script.aculo.us group.
To post to this group, send email to prototype-scriptacul...@googlegroups.com.
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en.



[Proto-Scripty] Re: Problem with prototype and arrays.

2010-08-02 Thread T.J. Crowder
Hi,

  Yes, it's an extremely common misconception.

 What, that for..in iterates over an object's properties?

No, that it loops over array indexes.

-- T.J.

On Aug 2, 3:49 pm, RobG rg...@iinet.net.au wrote:
 On Aug 2, 3:43 pm, T.J. Crowder t...@crowdersoftware.com wrote:

  Hi,

   I found that array looping method online, I guess I will remove that
   page from my bookmarks.

  Yes, it's an extremely common misconception.

 What, that for..in iterates over an object's properties? It's per
 ECMA-262. There are strategies for dealing with properties on an
 array's internal prototype chain so that for..in works just fine, even
 when it's built-in prototype has been modified.

 --
 Rob

-- 
You received this message because you are subscribed to the Google Groups 
Prototype  script.aculo.us group.
To post to this group, send email to prototype-scriptacul...@googlegroups.com.
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en.



[Proto-Scripty] Re: Problem with prototype and arrays.

2010-08-02 Thread RobG


On Aug 3, 2:19 am, T.J. Crowder t...@crowdersoftware.com wrote:
 Hi,

   Yes, it's an extremely common misconception.

  What, that for..in iterates over an object's properties?

 No, that it loops over array indexes.

I'm missing something here. Array indexes are properties, which is why
they are included in for..in iteration. The OPs problem was how to
deal with properties from the [[prototype]] chain, not that array
properties were included in for..in.

There are two issues to deal with when using for..in with a javascript
array:

 1. properties may have been added to Array.prototype, and

 2. object properties are not guaranteed to be returned in any
particular order

Both the above are common to any use of for..in. Item 1 is easily
dealt with. If order matters, a for, while or do loop can be used.
Otherwise, there is no issue.


--
Rob

-- 
You received this message because you are subscribed to the Google Groups 
Prototype  script.aculo.us group.
To post to this group, send email to prototype-scriptacul...@googlegroups.com.
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en.



[Proto-Scripty] Re: Problem with prototype and arrays.

2010-08-01 Thread T.J. Crowder
Hi,

`for..in` is not for looping through the indexes of an array, it's for
looping through the property names of an object. More in the API docs:

http://api.prototypejs.org/language/array/

...and in the unofficial wiki:

http://proto-scripty.wikidot.com/prototype:tip-looping-through-arrays

HTH,
--
T.J. Crowder
Independent Software Consultant
tj / crowder software / com
www.crowdersoftware.com


On Jul 31, 1:44 pm, Rog ro...@aidreams.co.uk wrote:
 Hi there everyone, I am pretty new to javascript but do a lot of other
 programming.

 Something is not working as expected.  Here's a bit of code I am
 testing :

 ==
 html
 head
 script type=text/javascript src=prototype1-1-6.js/script
 /head
 script

 var smileySet = new Array();

 smileySet['test'] = 'hello';
 for (var smiley in smileySet)
         document.write(smiley + ' = ' + smileySet[smiley] + 'br /');

 /script
 /html
 ===

 If I run that I get a screen full of the prototype javascript
 functions.  Without including prototype I just get the one array
 element 'test = hello' as expected.

 Why are all the prototype functions being added to the array ?

 Many thanks !

-- 
You received this message because you are subscribed to the Google Groups 
Prototype  script.aculo.us group.
To post to this group, send email to prototype-scriptacul...@googlegroups.com.
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en.



[Proto-Scripty] Re: Problem with prototype and arrays.

2010-08-01 Thread Rog
Thanks very much, that makes a lot of sense now.

I found that array looping method online, I guess I will remove that
page from my bookmarks.

Thanks for setting me right :)

On 1 Aug, 11:47, T.J. Crowder t...@crowdersoftware.com wrote:
 Hi,

 `for..in` is not for looping through the indexes of an array, it's for
 looping through the property names of an object. More in the API docs:

 http://api.prototypejs.org/language/array/

 ...and in the unofficial wiki:

 http://proto-scripty.wikidot.com/prototype:tip-looping-through-arrays

 HTH,
 --
 T.J. Crowder
 Independent Software Consultant
 tj / crowder software / comwww.crowdersoftware.com

-- 
You received this message because you are subscribed to the Google Groups 
Prototype  script.aculo.us group.
To post to this group, send email to prototype-scriptacul...@googlegroups.com.
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en.