<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// push方法是往数组的末尾添加新的元素,可以传递多个参数。返回是添加后的数组的长度
// 首先将方法定义再数组的原型上,这样数组都能去调用
Array.prototype.myPush = function(...items){//将参数转成数组
//遍历参数数组,依次在数组末尾添加上
for(let i=0;i<items.length;i++){
this[this.length] = items[i]
}
//返回数组的长度
return this.length
}
arr = [1,2,3]
const len = arr.myPush(1,2,3)
console.log(arr);
</script>
</body>
</html>