- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
There actually is a good reason for it to do that. JavaScript's type system does not consist of single values that can have multiple typed 'views' slid over them. JavaScript is dynamically typed in the sense that it automatically casts/converts a value by creating a new variable with the adapted type when necessary. It still requires values of the correct type though and the for...in loop construct iterates over object property names, i.e. , strings.
Try the following and you'll see you can't just access the same value as whatever type you may want:
The num variable starts out typed as a primitive Number. Writing members is something you can only do on Object variables, so num is type-converted into an Object. This creates a completely new value onto which the foo property is added. The original num variable's contents is left alone and if you try to alert num.foo later on, then it will return 'undefined' to reflect this.Moving onwards; there is not any real type conversion happening on the public facing side of the Array type. An Array in JavaScript is just another type of Object and still stores its indices as properties. You can even just borrow the Array prototype's methods and call them on regular objects. Provided a coherent length property exists on an object, it will even be updated to reflect added or removed 'indices':
From a public API point of view, arrays are still 'just' key->value maps. Internal storage is a different story though and browsers may offer optimization for contiguous stored indices on Array, where they don't on Object.Admin
Really? That doesn't sound very random to me....
Admin
also bad: '/files/design/' and '.jpg' are in the array and not in the write statement. Wasted memory and slow execution time
Admin
It doesn't "feel like placing it" somewhere - the <script> is probably in the right place, and it outputs the [image] right after it.
Admin
TRWTF is that that the last image in the array will never get selected.