?

Log in

No account? Create an account
wRog
Today's violation of the Principle of Least Surprise 
13th-Jul-2005 11:15 pm
toyz
  var answer = [];

  for (word in ['this','language','can','bite','me']) {
     answer.push(word);
  }
Just to make it easy, I'll tell you that the language in question is Javascript, [...] is an array constructor, and .push appends its arguments to the end of the list/array. The answer, of course, is...

  [0,1,2,3,4]
See, Javascript doesn't actually have arrays, Javascript has objects with numbered properties. And since foo.['whatever'] is defined to be foo.whatever with numbers and strings automagically converting to each other as needed, you'll never, ever notice the difference.

Unless, of course, you try to use for, which iterates through property names, not property values.

I suppose, to be completely fair, I should note that in Perl, some idiot could conceivably do
  for my $word (['this','language','can','bite','me']) {
     push @answer, $word;
  }
and get a list of length 1 for their trouble. Oh, well...
Comments 
15th-Jul-2005 06:18 am (UTC)
Well, it's a stack in that .pop() pops off the end, but if you iterate over the stack it would go from bottom to top, which is weird. (Unlike Lisp push/pop.)
This page was loaded Oct 23rd 2019, 5:44 pm GMT.