重写数组的七个响应式方法

const objProxy = reactive(obj)
const infoProxy = reactive(info)
const arrProxy = reactive([1,2,3,4])

function ReactiveArr() {
  const arr = Array.apply(this, arguments)
  arr.__proto__ = ReactiveArr.prototype
  return arr
}

Object.defineProperty(ReactiveArr, 'contructor', {
  enumerable: false,
  configurable: true,
  writable: true,
  value: ReactiveArr,
})

const ways = ['push', 'pop', 'shift', 'unshift', 'reverse', 'sort', 'splice']
ways.forEach(way => {
  ReactiveArr.prototype[way] = function () {
    const result = Array.prototype[way].apply(this, arguments)
    console.log(way, ': ' + arguments[0] + ', result: ', result)
    return result
  }
})

objProxy.movies = new ReactiveArr('不能说的秘密', '大灌篮', '头文字D')

console.log(objProxy.movies)

objProxy.movies.push('黑色幽默')
objProxy.movies.pop()
objProxy.movies.splice(0, 0, '搁浅')
objProxy.movies.shift()
objProxy.movies.unshift('爱在西元前', '龙拳')
objProxy.movies[0] = 'dasdasd'

/*
* 用class的形式
*             class OBArray extends Array {
                constructor (...args) {
                    super(...args);
                }
                push (...args) {
                    const len = super.push(...args);
                    console.log('push: ' + args + ' 新的数组长度为:' + len);
                    return len;
                }
            }
            const a = new OBArray(...[1,2]);
            a.push(20); // "push: 20 新的数组长度为:3"

*
*
* */

附上源码地址:vue的array方法重写

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue.js 2.x 中,为了实现响应式数组,你可以使用下面的方法重写数组方法: 1. push:使用 `Array.prototype.push` 方法,然后手动调用 `Vue.set` 更新数组,例如: ```javascript this.array.push(newItem); Vue.set(this.array, this.array.length - 1, newItem); ``` 2. pop:使用 `Array.prototype.pop` 方法,然后手动调用 `Vue.delete` 删除最后一个元素,例如: ```javascript const deletedItem = this.array.pop(); Vue.delete(this.array, this.array.length); ``` 3. shift:使用 `Array.prototype.shift` 方法,然后手动调用 `Vue.delete` 删除第一个元素,例如: ```javascript const deletedItem = this.array.shift(); Vue.delete(this.array, 0); ``` 4. unshift:使用 `Array.prototype.unshift` 方法,在调用之前手动调用 `Vue.set` 更新数组的长度,例如: ```javascript this.array.unshift(newItem); Vue.set(this.array, 0, newItem); ``` 5. splice:使用 `Array.prototype.splice` 方法,然后手动调用 `Vue.delete` 删除或添加元素,例如: ```javascript // 删除元素 const deletedItems = this.array.splice(startIndex, deleteCount); for (let i = 0; i < deleteCount; i++) { Vue.delete(this.array, startIndex + i); } // 添加元素 this.array.splice(startIndex, 0, ...newItems); for (let i = 0; i < newItems.length; i++) { Vue.set(this.array, startIndex + i, newItems[i]); } ``` 通过手动调用 `Vue.set` 和 `Vue.delete`,你可以确保数组的变化能够触发 Vue.js响应式更新。 注意:以上方法适用于 Vue.js 2.x,在 Vue.js 3.x 中,你可以直接使用原生数组方法,因为 Vue.js 3.x 支持了 Proxy 来实现更好的响应式机制。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值