Skip to content

Instantly share code, notes, and snippets.

@dwelch2344
Created September 24, 2012 19:56
Show Gist options
  • Save dwelch2344/3777978 to your computer and use it in GitHub Desktop.
Save dwelch2344/3777978 to your computer and use it in GitHub Desktop.
A simple JS List that extends the native Array structure and adds a remove(index) method
List = function(arr){
if( arr !== undefined){
if( arr instanceof Array == false){
for(var i in arguments){
this.push( arguments[i] );
}
}else{
for(var i in arr){
this.push( arr[i] );
}
}
}
}
List.prototype = new Array;
List.prototype.remove = function(i){
if( i == 0){
this.shift();
}else if(i > 0){
var first = this.splice(0, i-1);
var result = first.concat( this.splice(1, this.length - 1) );
while( this.length > 0 ) this.pop();
for( var e in result){
this.push( result[e] );
}
}
}
var l = new List(1,2,3,4,5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment