Created
September 24, 2012 19:56
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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