move misc to js/ui
This commit is contained in:
parent
594ac2b012
commit
4a48deeb2c
18 changed files with 4 additions and 4 deletions
23
1-js/99-js-misc/01-proxy/01-error-nonexisting/solution.md
Normal file
23
1-js/99-js-misc/01-proxy/01-error-nonexisting/solution.md
Normal file
|
@ -0,0 +1,23 @@
|
|||
|
||||
```js run
|
||||
let user = {
|
||||
name: "John"
|
||||
};
|
||||
|
||||
function wrap(target) {
|
||||
return new Proxy(target, {
|
||||
get(target, prop, receiver) {
|
||||
if (prop in target) {
|
||||
return Reflect.get(target, prop, receiver);
|
||||
} else {
|
||||
throw new ReferenceError(`Property doesn't exist: "${prop}"`)
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
user = wrap(user);
|
||||
|
||||
alert(user.name); // John
|
||||
alert(user.age); // Error: Property doesn't exist
|
||||
```
|
31
1-js/99-js-misc/01-proxy/01-error-nonexisting/task.md
Normal file
31
1-js/99-js-misc/01-proxy/01-error-nonexisting/task.md
Normal file
|
@ -0,0 +1,31 @@
|
|||
|
||||
# Error on reading non-existant property
|
||||
|
||||
Create a proxy that throws an error for an attempt to read of a non-existant property.
|
||||
|
||||
That can help to detect programming mistakes early.
|
||||
|
||||
Write a function `wrap(target)` that takes an object `target` and return a proxy instead with that functionality.
|
||||
|
||||
That's how it should work:
|
||||
|
||||
```js
|
||||
let user = {
|
||||
name: "John"
|
||||
};
|
||||
|
||||
function wrap(target) {
|
||||
return new Proxy(target, {
|
||||
*!*
|
||||
/* your code */
|
||||
*/!*
|
||||
});
|
||||
}
|
||||
|
||||
user = wrap(user);
|
||||
|
||||
alert(user.name); // John
|
||||
*!*
|
||||
alert(user.age); // Error: Property doesn't exist
|
||||
*/!*
|
||||
```
|
Loading…
Add table
Add a link
Reference in a new issue