#61 posted on Sun Oct 4 00:00:00 2009
JavaScript Array.length fails if key has line breaks
Sometimes JavaScript is just annoying. Here is something that caused me a lot of hair ripping trying to find a bug.
<script language="JavaScript">
var test = new Array();
test['\nfoo'] = 'bar';
test['bar'] = 'foo';
document.write(test.length+'<br>');
document.write(test['\nfoo']+'<br>');
document.write(test['bar']);
</script>This prints out:
0
bar
fooThe first number of course should be 2, since the array has two entries.
The same code in Python:
test = {'\nfoo':'bar','bar':'foo'}
print len(test)
print test['\nfoo']
print test['bar']Prints out:
2
bar
foo
So JavaScript array.length can't handle line breaks in the key section, which is kinda odd. A little Googling didn't really give me any answers if this is a feature or a bug. If it is a feature then it's damn stupid that the array.length call doesn't give an error out.












Comments (post a comment)