ippa/jaws — Issue #26: SpriteList still drawing elements after they're removed
Author: dmitrizagidulin
Created: 2012-01-10 16:33:16+00:00
State: open

Hi ippa,

So after beating my head against the wall on this issue all last night, this is what I found out.

If you add a sprite to a SpriteList, and then remove it, and then call draw() on the sprite list,
the removed sprite is still drawn.

What this actually boils down to is:
```javascript
list = new SpriteList
list.push('a')
list[0] //=> 'a'
list.splice(0, 1)
list.length //=> 0. But!
list[0] //=> 'a', still
```
You can see a more detailed demo here:
https://github.com/dmitrizagidulin/IronSanta/blob/c03fdaeb2ad6c48475e3d082751bc961867ca692/sprite_list_issue.html

And the reason for THAT behavior is, basically, you can't subclass Array, in javascript, as this excellent article explains:
http://perfectionkills.com/how-ecmascript-5-still-does-not-allow-to-subclass-an-array/

Meaning, it's not just the SpriteList. You can duplicate this behavior by creating any subclass of Array (ie creating a new object and setting Array as its prototype, as JawsJS does). The resulting "subclass" of Array will not behave properly with regards to various methods, including splice()

So.. would you accept a patch to rewrite SpriteList using composition instead of inheritance? (that is, a SpriteList would have an attribute called 'contents', which would be an actual array, which would store the list of sprites, and delegate its calls to it, etc)