オリジナルjsパッケージ配列API pop、push、reverse、sort、shift、unshiftなど、フロントペンの問題
1752 ワード
1. ソートを並べ替えます
4. 出庫pop
Array.prototype.sorts=function(){
for(var i=0;i
2. リバース Array.prototype.newReverse= function(){
for(i=0,j=this.length-1;j>i;j--,i++){
var temp = this[i]
this[i] = this[j]
this[j] = temp
}
return this
}
var a = [1,2,3,4,5,6,7,8]
a.newReverse()
console.log(a)
3. 倉庫に入る Array.prototype.myPush = function(item){
this[this.length] = item;
return this.length
}
var a = [1,2,3,4]
a.myPush(5)
console.log(a)
4. 出庫pop
Array.prototype.myPop = function(){
var b=this.length;
var result = this[b-1];
this.length = b - 1;
return result;
}
var a = [1,2,3,4]
console.log(a.myPop())
console.log(a);
5.入隊unshift
Array.prototype.myUnshift = function(item){
this.length = this.length+1
for(var i=this.length-1;i>=0;i--){
this[i]=this[i-1]
}
this[0]=item
return this.length
}
var a = [1,2,3,4]
a.myUnshift(100)
console.log(a)
6.出隊シフト Array.prototype.myShift = function(){
var result = this[0]
for(var i=0;i