Skip to content

Instantly share code, notes, and snippets.

@chicagoworks
Created December 26, 2010 17:09
Show Gist options
  • Save chicagoworks/755512 to your computer and use it in GitHub Desktop.
Save chicagoworks/755512 to your computer and use it in GitHub Desktop.
Array object additions
//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