ECMAScript 6 proxies bring intercession to
JavaScript. They work as follows. There are
many operations that you can perform on an
object obj. For example:
Getting the property prop of an object obj (obj.prop)
Checking whether an object obj has a property prop
('prop' in obj)
Proxies are special objects that allow you customize some of these
operations. A proxy is created with two parameters:
handler: For each operation, there is a corresponding handler
method that – if present – performs that operation. Such a method
intercepts the operation (on its way to the target) and is called
a trap (a term borrowed from the domain of operating systems).
target: If the handler doesn’t intercept an operation then it is
performed on the target. That is, it acts as a fallback for the
handler. In a way, the proxy wraps the target.
Examples
In the following example the handler intercepts the operations get and
has:
Use cases for proxies
Tracing property acesses (get, set)
Warning about unknown properties (get,set)
When it comes to accessing properties, JavaScript is very forgiving. For
example, if you try to read a property and misspell its name, you don’t
get an exception, you get the result undefined. You can use proxies to
get an exception in such a case. This works as follows. We make the
proxy a prototype of an object.
If a property isn’t found in the object, the get trap of the proxy is
triggered. If the property doesn’t even exist in the prototype chain
after the proxy, it really is missing and we throw an exception.
Otherwise, we return the value of the inherited property. We do so by
forwarding the get operation to the target (the prototype of the
target is also the prototype of the proxy).
Unknown Property Examples
Object
Class
If we turn PropertyChecker into a constructor, we can use it for
classes via extends:
Negative Array indices (get)
Some array prototype
methods let you
refer to the last element via -1, to the second-to-last element via -2,
etc. For example:
Alas, that doesn’t work when accessing elements via the bracket operator
([]). We can, however, use proxies to add that capability. The
following function createArray() creates Arrays that support negative
indices. It does so by wrapping proxies around Array instances. The
proxies intercept the get operation that is triggered by the bracket
operator.
Data binding (set)
Accessing a restful web service
Recoverable references
Revocable references work as follows: A client is not allowed to
access an important resource (an object) directly, only via a reference
(an intermediate object, a wrapper around the resource). Normally, every
operation applied to the reference is forwarded to the resource. After
the client is done, the resource is protected by revoking the reference,
by switching it off. Henceforth, applying operations to the reference
throws exceptions and nothing is forwarded, anymore.
In the following example, we create a revocable reference for a
resource. We then read one of the resource’s properties via the
reference. That works, because the reference grants us access. Next, we
revoke the reference. Now the reference doesn’t let us read the
property, anymore.