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

docs(proxy): add advanced proxy usage example with Reflect #129

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
docs(proxy): add advanced proxy usage example with Reflect
  • Loading branch information
JayKan committed Mar 15, 2018
commit 79608b5bc30541dccaca87a707659e57cbe4d38b
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,27 @@ var p = new Proxy(target, handler);
p() === 'I am the proxy';
```

```JavaScript
// Advanced field interception with Proxy and the Reflect object
// Reflect is a built-in object that provides methods for interceptable
// JavaScript operation. The methods are the same as those of proxy
// handlers. Reflect is not a function object, so it's not constructable.

var advance = new Proxy({}, {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

advanced I think?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch @hlomzik 👍

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See b559ef6 for changes

get(target, property, receiver) {
console.log(`get called for field: ${property}`);
return Reflect.get(target, property, receiver);
},
set(target, property, value, receiver) {
console.log(`set called for field: ${property} and value: ${value}`);
return Reflect.set(target, property, value, receiver);
}
});

advanced.firstName = 'foo';
advanced.lastName ='bar';
```

There are traps available for all of the runtime-level meta-operations:

```JavaScript
Expand Down