JS知识点总结(九)

JS知识点总结

文章目录

一、如何监听数组的改变?

利用Proxy可以来监听数组的length属性,当push或者pop的时候,会触发setter

const proxyArr = new Proxy(unkownArr, {
    set(target, prop, value, receiver) {
        if (prop === 'length') {
            console.log('set new value', value);
            return false;
        }
        console.log('receiver', receiver);
        return Reflect.set(...arguments);
    }
});

那要监听数组某一项的变化呢?那就是prop !== 'length'的情况了:

const proxyArr = new Proxy(unkownArr, {
    set(target, prop, value, receiver) {
        if (prop !== 'length') {
            console.log('set new value', value);
            return false;
        }
        console.log('receiver', receiver);
        return Reflect.set(...arguments);
    }
});
上一篇:el-table-column prop中如何使用三目表达


下一篇:Invalid prop: custom validator check failed for prop “index“. 解决