-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextend.js
More file actions
34 lines (29 loc) · 908 Bytes
/
extend.js
File metadata and controls
34 lines (29 loc) · 908 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* Created by Administrator on 2015/9/1.
*/
Array.prototype.movePos=function(n,m){
n=n<0?0:(n>this.length-1?this.length-1:n);
m=m<0?0:(m>this.length-1?this.length-1:m);
var temp;
if(n===m){
return this;
}
else{
if(n>m)//向前移动>对两个索引位置及其中间的元素重新赋值[顺推]
{
temp=[this[m],this[m]=this[n]][0];//交换n和m的值并将m上的值赋给temp
for(var i=m+1; i<=n; i++)
{
temp=[this[i],this[i]=temp][0];
}
}
else{//向后移动>对两个索引位置及其中间的元素重新赋值[倒推]
temp=[this[m],this[m]=this[n]][0];//交换n和m的值并将m上的值赋给temp
for(var i=m-1; i>=n; i--)
{
temp=[this[i],this[i]=temp][0];
}
}
return this;
}
};