improvements
This commit is contained in:
parent
67e317cee9
commit
52ef3256c9
16 changed files with 100 additions and 96 deletions
|
@ -1,6 +1,6 @@
|
|||
The solution consists of two parts:
|
||||
|
||||
1. Whenever `.observe(handler)` is called, we need to remember the handler somewhere, to be able to call it later. We can store it right in the object, using our symbol as the key.
|
||||
1. Whenever `.observe(handler)` is called, we need to remember the handler somewhere, to be able to call it later. We can store handlers right in the object, using our symbol as the property key.
|
||||
2. We need a proxy with `set` trap to call handlers in case of any change.
|
||||
|
||||
```js run
|
||||
|
|
|
@ -20,10 +20,8 @@ user.observe((key, value) => {
|
|||
user.name = "John"; // alerts: SET name=John
|
||||
```
|
||||
|
||||
In other words, an object returned by `makeObservable` has the method `observe(handler)`.
|
||||
In other words, an object returned by `makeObservable` has the method `observe(handler)` that allows to add `handler` function to be called on a property change.
|
||||
|
||||
Whenever a property changes, `handler(key, value)` is called with the name and value o the property.
|
||||
|
||||
|
||||
P.S. In this task, please handle only writing to a property. Other operations can be implemented in a similar way.
|
||||
P.P.S. You might want to introduce a global variable or a global structure to store handlers. That's fine here. In real life, such function lives in a module, that has its own global scope.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue