Object.definePropert 解决Array劫持问题
重写Array的原型方法
const orginalProto = Array.prototype; const arrayProto = Object.create(orginalProto); // 先克隆一份Array的原型出来 const methodsToPatch = [ 'push', 'pop', 'shift', 'unshift', 'splice', 'sort', 'reverse' ] methodsToPatch.forEach(method => { arrayProto[method] = function () { // 执行原始操作 orginalProto[method].apply(this, arguments) console.log('监听赋值成功', method) } })

浙公网安备 33010602011771号