Proxy()
The Proxy()
constructor is used to create Proxy
objects.
Syntax
new Proxy(target, handler)
Note:
Proxy()
can only be constructed withnew
. Attempting to call it withoutnew
throws aTypeError
.
Parameters
target
- : A target object to wrap with
Proxy
. It can be any sort of object, including a native array, a function, or even another proxy.
- : A target object to wrap with
handler
- : An object whose properties are functions that define the behavior of the proxy when an operation is performed on it.
Description
Use the Proxy()
constructor to create a new Proxy
object.
This constructor takes two mandatory arguments:
target
is the object for which you want to create the proxyhandler
is the object that defines the custom behavior of the proxy.
An empty handler will create a proxy that behaves, in almost all respects, exactly like
the target. By defining any of a set group of functions on the handler
object, you can customize specific aspects of the proxy's behavior. For example, by
defining get()
you can provide a customized version of the target's
property accessor.
Handler functions
This section lists all the handler functions you can define. Handler functions are sometimes called traps, because they trap calls to the underlying target object.
handler.apply()
- : A trap for a function call.
handler.construct()
- : A trap for the
new
operator.
- : A trap for the
handler.defineProperty()
- : A trap for
Object.defineProperty
.
- : A trap for
handler.deleteProperty()
- : A trap for the
delete
operator.
- : A trap for the
handler.get()
- : A trap for getting property values.
handler.getOwnPropertyDescriptor()
)}- : A trap for
Object.getOwnPropertyDescriptor
.
- : A trap for
handler.getPrototypeOf()
- : A trap for
Object.getPrototypeOf()
.
- : A trap for
handler.has()
- : A trap for the
in
operator.
- : A trap for the
handler.isExtensible()
- : A trap for
Object.isExtensible()
.
- : A trap for
handler.ownKeys()
- : A trap for
Object.getOwnPropertyNames()
andObject.getOwnPropertySymbols()
.
- : A trap for
handler.preventExtensions()
)}- : A trap for
Object.preventExtensions()
.
- : A trap for
handler.set()
- : A trap for setting property values.
handler.setPrototypeOf()
- : A trap for
Object.setPrototypeOf()
.
- : A trap for