<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
//Object.create返回一个新对象,新对象的_proto_就是传进去的参数
let newProtetype = Object.create(Array.prototype);
let methods = ["push", "pop", "shift", "unshift", "reserve", "sort", "splice"];
methods.forEach(method => {
//新原型上添加同名[method] ,返回原来的Array.prototype[method]
newProtetype[method] = function (...args) {
console.log(`使用了${method}方法`);
return Array.prototype[method].call(this, ...args);
}
})
let arr = [1, 2, 3];
//绑定新的原型
arr.__proto__ = newProtetype;
arr.push(90);//向数组的末尾添加一个或更多元素
arr.pop();// 删除数组的最后一个元素
arr.unshift(34);//将值添加到数组开头
arr.shift();//删除第一个
arr.reverse();//颠倒数组中元素的顺序
console.log(arr)
</script>
</body>
</html>
笔试--js监听数组变化
最新推荐文章于 2024-01-05 17:20:15 发布