We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
实现一个 Reactive 方法,返回可监听 key 变化的函数
// 实现 function Reactive() { // ... } // 测试 const obj = { a: 1, b: 2 }; const reactive = Reactive(obj); reactive.subscribe((key, oldValue, newValue) => { console.log(key, oldValue, newValue); }); reactive.state.a = 3; // console.log('a', 1,2) reactive.state.c = 10; //console.log('c', undefined, 10)
The text was updated successfully, but these errors were encountered:
function Reactive(obj) { handlers = []; const subscribe = (handler) => { handlers.push(handler); }; const notify = (key, oldValue, newValue) => { handlers.forEach((handler) => handler(key, oldValue, newValue)); }; const proxy = new Proxy(obj, { set(target, key, value) { if (value !== target[key]) { notify(key, target[key], value); target[key] = value; } return true; }, }); return { subscribe, state: proxy, }; }
Sorry, something went wrong.
No branches or pull requests
实现一个 Reactive 方法,返回可监听 key 变化的函数
The text was updated successfully, but these errors were encountered: