<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script>
let obj = {
2:3,
3:4,
length:2,
push:Array.prototype.push
}
obj.push(1) //相当于obj[2] = 1
obj.push(2) //相当于obj[3] = 2
console.log(obj) //{2:1,3:2,length:4,push:f}
//弄懂这个题总重要的一点是写出数组原型的push方法
function my_push(x){
//自己的长度为索引赋值
this[this.length] = x
//此处还需注意length会自动加一
return this.length
}
</script>
</body>
</html>
let obj = { 2:3, 3:4, length:2, push:Array.prototype.push}
最新推荐文章于 2022-10-08 23:02:36 发布