场景import { observe } from "./reactive";
import Watcher from "./watcher";
const data = {
list: ["hello"],丝抽
};
observe(data);
const updateComponent = () => {
for (const item of data.list) {
console.log(item);
}
};
new Watcher(updateComponent);
data.list = ["hello", "liang"];先可以一分钟思考下会输出什么。
... ...
虽然 list 的茧响值是数组,但我们是应式对 data.list 进行整体赋值,所以依旧会触发 data.list 的系统 set ,触发 Watcher 进行重新执行,免费信息发布网数组输出如下:

场景 2import { observe } from "./reactive";
import Watcher from "./watcher";
const data = {
list: ["hello"],丝抽
};
observe(data);
const updateComponent = () => {
for (const item of data.list) {
console.log(item);
}
};
new Watcher(updateComponent);
data.list.push("liang");先可以一分钟思考下会输出什么。
... ...
这次是茧响调用 push 方法,但我们对 push 方法什么都没做,应式因此就不会触发 Watcher 了。系统
方案为了让 push 还有数组的数组其他方法也生效,我们需要去重写它们,香港云服务器丝抽通过 代理模式,茧响我们可以将数组的应式原方法先保存起来,然后执行,系统并且加上自己额外的数组操作。
/
** not type checking this file because flow doesnt play well with
* dynamically accessing methods on Array prototype
*/
/
*export function def(obj, key, val, enumerable) {
Object.defineProperty(obj, key, {
value: val,
enumerable: !!enumerable,
writable: true,
configurable: true,
});
}
*/
import { def } from "./util";
const arrayProto = Array.prototype;
export const arrayMethods = Object.create(arrayProto);
const methodsToPatch = [
"push",
"pop",
"shift",
"unshift",
"splice",
"sort",
"reverse",
];
/
*** Intercept mutating methods and emit events
*/
methodsToPatch.forEach(function (method) {
// cache original method
const original = arrayProto[method];
def(arrayMethods, method, function mutator(...args) {
const result = original.apply(this, args);
/
相关文章:
源码库服务器租用IT技术网IT资讯网香港云服务器企商汇源码下载益华科技亿华云益强编程舍益强IT技术网云智核科技前瞻技术快报创站工坊运维纵横智能时代益强科技亿华智造汇智坊益华科技码力社亿华互联思维库亿华科技极客编程全栈开发亿华云计算码上建站IT资讯网益强智囊团益强智未来多维IT资讯益强科技亿华灵动益华IT技术论坛益强编程堂益华科技益强前沿资讯极客码头亿华云编程之道益强数据堂益强资讯优选云站无忧
5.7921s , 11784.21875 kb
Copyright © 2025 Powered by Vue2剥丝抽茧-响应式系统之数组,亿华互联 滇ICP备2023000592号-16