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 all commits
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
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 advanced = new Proxy({}, {
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