• (cs) in reply to anonymous
    anonymous:
    Ragnax:
    anonymous:
    Ok... -5 gets type-converted to string because it's "different". What do you think this will result in, then?

    var arr = []; arr[1] = "test" for (var i in arr) { alert(typeof i); }

    Trick question. That depends on whether or not someone has been messing with Array.prototype beforehand. Assuming no-one has, it will alert "string" once.

    That was the assumption, and that's correct, it alerts "string". So TRWTF is that in Javascript, array indices are stored as strings?

    Unless for...in is explicitly casting the value to a string. There's no good reason for it to do that, since Javascript is loosely typed, but you never know...

    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:

    var num = 2;
    num.foo = "bar";
    alert(num.foo); // alerts 'undefined'
    
    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':

    var obj = { length : 0 };
    Array.prototype.push.call(obj, "foo", "bar");
    alert( obj.length ); // alerts '2'
    
    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.
  • Nobody (unregistered) in reply to faoileag
    I mean, a random image usually is expected to appear at a specific place inside the webpage, not wherever document.write feels like placing it.

    Really? That doesn't sound very random to me....

  • Sven (unregistered)

    also bad: '/files/design/' and '.jpg' are in the array and not in the write statement. Wasted memory and slow execution time

  • Mr. TA (unregistered) in reply to faoileag

    It doesn't "feel like placing it" somewhere - the <script> is probably in the right place, and it outputs the [image] right after it.

  • Dan F (unregistered)

    TRWTF is that that the last image in the array will never get selected.

Leave a comment on “Indexed Image”

Log In or post as a guest

Replying to comment #:

« Return to Article