Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【74页】watch 的实现原理 建议增加读者注意 #46

Open
w457130328 opened this issue Feb 27, 2022 · 1 comment
Open

【74页】watch 的实现原理 建议增加读者注意 #46

w457130328 opened this issue Feb 27, 2022 · 1 comment

Comments

@w457130328
Copy link

目前书中所实现的代码,只能在监听有返回的 getter 函数时,才能将 newValue,oldValue 正确传入,其他情况都是 undefined。
有返回的函数是指:

// 1. 简写箭头函数
() => obj.foo; // 收集 obj.foo 的值为 newValue, oldValue

// 2. 完整写法
() => {return obj.foo};

// 3. 监听多个 getter
// 收集 obj.bar 的值为 newValue, oldValue;如果没有返回值,则 newValue 和 oldValue 都是 undefined
() => {obj.foo; return obj.bar};

测试示例图:

image

原因分析:

代码中 newValueoldValue 都是通过 effectFn() 的执行结果赋值的。而 effectFn() 的执行结果又依赖于 副作用函数 的执行结果。在当前上下文中 getter() 的返回值决定了 newValue,oldValue 的值

image

@mango2630
Copy link

这里是不是 traverse 实现有问题导致的,我没理解 traverse 函数在做什么用;
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants