Created
December 26, 2010 17:09
-
-
Save chicagoworks/755512 to your computer and use it in GitHub Desktop.
Array object additions
This file contains 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
//implement shift and unshift on Array prototype | |
//http://www.javascriptkit.com/javatutors/oopjs2.shtml | |
if(!Array.prototype.shift) { // if this method does not exist.. | |
Array.prototype.shift = function(){ | |
firstElement = this[0]; | |
this.reverse(); | |
this.length = Math.max(this.length-1,0); | |
this.reverse(); | |
return firstElement; | |
} | |
} | |
if(!Array.prototype.unshift) { // if this method does not exist.. | |
Array.prototype.unshift = function(){ | |
this.reverse(); | |
for(var i=arguments.length-1;i>=0;i--){ | |
this[this.length]=arguments[i] | |
} | |
this.reverse(); | |
return this.length | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment