1、myPush()
//myPush() 數組入棧
Array.prototype.myPush = function(){
var L = this.length;
for(var i = L ; i < L + arguments.length ; i++){
this[i] = arguments[i - L];
}
return this.length;
}
2、myPop()
//Mypop() 數組出棧
Array.prototype.myPop = function(){
if (this.length == 0) {
return undefined;
}
var last = this[this.length-1];
this.length = this.length-1;
return last;
}
3、myUnshitf()
//myUnshift 數組入隊
Array.prototype.myUnshift = function(){
var L = this.length;
for(var i = L + arguments.length - 1 ; i >= 0 ; i--){
if(i > arguments.length - 1){
this[i] = this[i - arguments.length];
}else{
this[i] = arguments[i];
}
}
return this.length;
}
4、myShitf()
//myShift()數組出隊
Array.prototype.myShift = function(){
if (this.length == 0) {
return undefined;
}
var firstElement = this[0];
for(var i = 1 ; i < this.length ; i++){
this[i-1] = this[i];
}
this.length = this.length-1;
return firstElement;
}
純手寫,若有不善,請指正 ^_^