- 重写prototype,一般用于做数据拦截
let newProtos = Object.create(Array.prototype)
newProtos.push = function () {
console.log('can do something,被拦截了')
// return Array.prototype.push.apply(this, arguments)
return [].push.apply(this, arguments)
}
let arr = [1, 2, 3, 6]
// 被拦截
arr.__proto__ = newProtos
console.log(arr.push(8)) //5
console.log(arr) // [ 1, 2, 3, 6, 8 ]
// 没被拦截
let arr2 = [10, 11, 12, 13, 14]
console.log(arr2.push(16)) //6
console.log(arr2) // [ 10, 11, 12, 13, 14, 16 ]