对数组对象方法的重写练习
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
function Marry(){
this.length=arguments.length;
for (var i=0;i<arguments.length;i++){
this[i]=arguments[i];
}
this.pop=function (){
var popData=this[this.length-1];
delete this[this.length-1];
this.length --;
return popData;
}
this.push = function (){
for (i=0;i<arguments.length;i++){
this[this.length]=arguments[i];
this.length++
}
return this.length;
}
this.shift =function (){
var shiftData=this[0];
delete this[0];
this.length--;
for (var i=0;i<this.length;i++)
{
this[i] = this[i+1];
}
delete this[this.length];
return shiftData;
}
this.unshift = function (){
var sum =this.length +arguments.length;
for (i=sum-1;i>= arguments.length;i--){
this[i]=this[i-arguments.length];
}
for (i=0;i<arguments.length;i++){
this[i] =arguments[i];
this.length++;
}
return this.length
}
this.MtoString = function (){
var result = '';
for (i=0;i<this.length-1;i++)
{
result = result +this[i]+',';
}
result+=this[i];
return result;
}
}
arr=new Marry('zhangsan','lisi','wangwu','maqi');
console.log(arr.length);
console.log(arr);
document.write(arr[0]);
console.log(arr.pop());
console.log(arr.length);
console.log(arr);
console.log(arr.push('tt','wqwd','asdasd'));
console.log(arr.push('yu'));
console.log(arr);
//
console.log(arr);
console.log(arr.shift());
console.log(arr);
console.log(arr.unshift('tuyi','nmb','hujing'));
console.log(arr);
console.log(arr.MtoString());
</script>
</body>
</html>