In my situation, I am dealing with "scene" and various graphics "objects"...
Scene.prototype.objects=new Array();
Scene.prototype.add=function(obj){
var last=this.objects.length;
this.objects[last]=obj}
Scene.prototype.remove=function(obj){
this.objects.splice(obj.id,1)}
Scene.prototype.advance=function(){
for (var id in this.objects){
var obj=this.objects[id];
obj.id=id;
obj.advance();
}
}
Scene.prototype.paint=function(context){...}
As I repeatedly create and remove many objects, I am curious if there is a more efficient way to handle this in JavaScript. I know that Array.prototype.splice re-indexes the array when items are removed, but is there a better technique for adding and removing items?
In my opinion, another approach could be something like
Scene.prototype.remove=function(obj){
delete this.objects[obj.id]; // omitting concern for this.objects.length
delete obj; // possibly unnecessary...
}
I have yet to experiment with this alternative method...
If anyone can recommend a good book on JavaScript, I would greatly appreciate it :)