Object.defineProperty
The ** object.defineProperty ()** method directly defines a new property on an Object, or modifies an existing property of an Object, and returns the Object.
Syntax: Object.defineProperty(obj, prop, Descriptor)
Parameters:
Obj: Object for which attributes are to be defined.
Prop: The name or Symbol of the property to be defined or modified.
Descriptor: Property descriptor to define or modify.
Return value: The object passed to the function.
describe
This method allows you to add or modify attributes of an object precisely. Common attributes added through assignment operations are enumerable and are enumerated to (for… In or object. keys methods), you can change the values of these properties, or you can delete them. This method allows you to modify the default additional options (or configurations). By default, attribute values added using Object.defineProperty() are immutable.
There are two main types of property descriptors that currently exist in objects: data descriptors and access descriptors. A data descriptor is a property with a value that can be writable or unwritable. Access descriptors are properties described by getter and setter functions. A descriptor can only be one of these two; You can’t be both.
Both descriptors are objects. They share the following optional key values (the default is the default when defining attributes using Object.defineProperty()) :
-
configurable
The descriptor of this property can be changed and deleted from the corresponding object only when the property’s key is set to true. The default is false.
-
enumerable
The property appears in the object’s enumerable property if and only if its Enumerable key value is true. The default is false.
The data descriptor also has the following optional key values:
-
value
The value corresponding to this property. It can be any valid JavaScript value (numeric value, object, function, etc.). The default is undefined.
-
writable
The property’s value, the value above, can be changed by the assignment operator if and only if the property’s writable key is true. The default is false.
The access descriptor also has the following optional key values:
-
get
Property, or undefined if there is no getter. This function is called when the property is accessed. No arguments are passed, but this object is passed (because of inheritance, this is not necessarily the object that defines the property). The return value of this function is used as the value of the property. The default is undefined.
-
set
Property, or undefined if there is no setter. This function is called when the property value is modified. This method takes an argument (that is, the new value being assigned) and passes in the this object at the time of assignment. The default is undefined.
Summary of descriptor defaults
- A key with a Boolean value
configurable
,enumerable
和writable
Is the default value offalse
. - The key of the property value and function
value
,get
和set
The default value of the field isundefined
.
The key values that the descriptor can have
configurable``enumerable``value``writable``get``set
Data descriptor can can can can can not can not access descriptor can can can can can can
A descriptor is considered a data descriptor if it does not have any of the keys of value, writable, GET, and set. An exception is raised if a descriptor has both value or writable and get or set keys.
Proxy
Proxy objects are used to define custom behavior for basic operations (such as property lookups, assignments, enumerations, function calls, and so on).
grammar
const p = new Proxy(target, handler)
parameter
-
target
The target object (which can be any type of object, including a native array, function, or even another Proxy) that you want to wrap with a Proxy.
-
handler
An object that typically has functions as attributes, and the functions in each attribute define the behavior of agent P when performing various operations.
methods
-
Proxy.revocable()
Create an undoable Proxy object.
Handler object method
The Handler object is a placeholder object that holds a set of specific properties. It contains the various traps of the Proxy.
All catchers are optional. If a catcher is not defined, the default behavior of the source object is retained.
-
handler.getPrototypeOf()
: is a Proxy method that is called when the prototype of a Proxy object is read.grammar
const p = new Proxy(obj, { getPrototypeOf(target) { ... }});Copy the code
parameter
When the getPrototypeOf method is called, this refers to the handler object to which it belongs.
-
target
The target object to be proxied.
The return value
The return value of the getPrototypeOf method must be an object or NULL.
Object.getPrototypeOf
Method. Returns the prototype of the specified objectgrammar
Object.getPrototypeOf(object) Copy the code
Parameter: obj The object to return its prototype.
Return value: The prototype of the given object. If there is no inherited attribute, null is returned.
-
-
handler.setPrototypeOf()
The: method is mainly used for interceptionObject.setPrototypeOf()
.grammar
var p = new Proxy(target, { setPrototypeOf: function(target, prototype) {}});Copy the code
parameter
The following arguments are passed to the setPrototypeOf method.
-
target
Intercepted target object.
-
prototype
Object new prototype or null.
The return value
The setPrototypeOf method returns true if the [[Prototype]] modification was successful, false otherwise.
Object.setPrototypeOf
Method. Sets the Prototype of a specified object (that is, the internal [[Prototype]] property) to another object ornull
.grammar
Object.setPrototypeOf(obj, prototype) Copy the code
parameter
-
obj
The object for which you want to set the prototype. .
-
prototype
A new prototype for the object (an object or NULL).
describe
If the Object’s [[Prototype]] is modified to be unextensible (viewed with Object.isextensible ()), TypeError is raised. If the prototype argument is not an object or null(for example, a number, string, Boolean, or undefined), then nothing is done. Otherwise, the method changes obj’s [[Prototype]] to the new value.
Object.setprototypeof () is a method in the latest draft of ECMAScript 6 that is considered a more appropriate way to modify Object prototypes than object.prototype.__proto__
-
-
handler.isExtensible()
: Used to intercept object.isextensible () on objects.grammar
var p = new Proxy(target, { isExtensible: function(target) {}});Copy the code
parameter
The following parameters will be passed to the isExtensible method. This is bound to the handler object.
-
target
Target object.
The return value
The isExtensible method must return a Boolean value or a value that can be converted to Boolean.
Object. IsExtensible method of the catcher.
-
-
handler.preventExtensions()
: Used to set pairsObject.preventExtensions()
The interceptgrammar
var p = new Proxy(target, { preventExtensions: function(target) {}});Copy the code
parameter
The following parameters are passed to the preventExtensions method. It’s going to bind to this handler.
-
target
The target object to intercept.
The return value
The preventExtensions method returns a Boolean value.
The catcher for the object. preventExtensions method.
-
-
handler.getOwnPropertyDescriptor()
Is:Object.getOwnPropertyDescriptor()
The hook.grammar
var p = new Proxy(target, { getOwnPropertyDescriptor: function(target, prop) {}});Copy the code
parameter
The following arguments are passed into the getOwnPropertyDescriptor method. This is binding to the handler.
-
target
Target object.
-
prop
Returns a description of the property name.
The return value
The getOwnPropertyDescriptor method must return an object or undefined.
Object. GetOwnPropertyDescriptor trap method.
-
-
Handler.defineproperty () : Intercepts the object.defineProperty () operation on an Object.
grammar
var p = new Proxy(target, { defineProperty: function(target, property, descriptor) {}});Copy the code
parameter
The following arguments will be passed to the defineProperty method. This is bound to the handler object.
-
target
Target object.
-
property
The name of the attribute whose description is to be retrieved.
-
descriptor
The descriptor of the property to be defined or modified.
The return value
The defineProperty method must return a Boolean indicating whether the operation defining the property was successful or not.
A catcher for the object.defineProperty method.
-
-
Handler.has () : is a proxy method for the in operator.
grammar
var p = new Proxy(target, { has: function(target, prop) {}});Copy the code
parameter
The following are the arguments passed to the HAS method.
-
target
Target object.
-
prop
Properties that need to be checked for existence.
The return value
The HAS method returns the value of a Boolean property.
intercept
This hook intercepts the following operations:
- Attribute query:
foo in proxy
- Inherited attribute query:
foo in Object.create(proxy)
with
check: with(proxy) { (foo); }
Reflect.has()
-
-
Handler.get () : Used to intercept read property operations on objects.
grammar
var p = new Proxy(target, { get: function(target, property, receiver) {}});Copy the code
parameter
The following are the parameters passed to the GET method. The this context is bound to the Handler object.
-
target
Target object.
-
property
The name of the property to be obtained.
-
receiver
Proxy or objects that inherit from Proxy
The return value
The get method can return any value.
intercept
This method intercepts the following operations on the target object:
- Access properties:
The proxy/foo and
proxy.bar
- Access properties on the prototype chain:
Object.create(proxy)[foo]
Reflect.get()
-
-
Handler.set () : is the catcher for the set property value operation.
grammar
const p = new Proxy(target, { set: function(target, property, value, receiver) { } }); Copy the code
parameter
Here are the arguments passed to the set() method. This is bound to the handler object.
-
target
Target object.
-
property
The name of the property or Symbol to be set.
-
value
New property value.
-
receiver
The object that was originally called. This is usually the proxy itself, but it is also possible that the handler’s set method could be called indirectly (and therefore not necessarily the proxy itself) on the prototype chain or in some other way. * * such as: Name = “jen”. Obj is not a proxy and does not have a name attribute, but it has a proxy on its prototype chain. Then the proxy’s set() handler will be called. Obj is passed in as the receiver parameter.
The return value
The set() method should return a Boolean value.
- return
true
Indicates that the property is set successfully. - In strict mode, if
set()
Method returnsfalse
, then one will be thrownTypeError
The exception.
-
-
Handler.deleteproperty () : Used to intercept delete operations on object properties.
grammar
var p = new Proxy(target, { deleteProperty: function(target, property) {}});Copy the code
parameter
The deleteProperty method will accept the following parameters. This is bound to the handler.
-
target
Target object.
-
property
Name of the attribute to be deleted.
The return value
DeleteProperty must return a Boolean value indicating whether the property was successfully deleted.
-
-
Handler.ownkeys () : Used to intercept reflect.ownkeys ().
grammar
var p = new Proxy(target, { ownKeys: function(target) {}});Copy the code
parameter
The following arguments are passed to ownKeys. This is bound to the handler.
-
target
Target object.
The return value
The ownKeys method must return an enumerable object.
-
-
Handler.apply () : Used to intercept calls to functions.
grammar
var p = new Proxy(target, { apply: function(target, thisArg, argumentsList) {}});Copy the code
parameter
The following are the parameters passed to the Apply method, and the this context is bound to the handler object.
-
target
Target object (function).
-
thisArg
The context object when called.
-
argumentsList
The array of arguments when called.
The return value
The Apply method can return any value.
A catcher for a function call operation.
-
-
Handler.construct () : Used to intercept the new operator. In order for the new operator to work on the generated Proxy object, the target object used to initialize the Proxy must itself have the [[Construct]] internal method (that is, the new target must be valid).
grammar
var p = new Proxy(target, { construct: function(target, argumentsList, newTarget) {}});Copy the code
parameter
The following argument is passed to the construct method, which is bound to the handler.
-
target
Target object.
-
argumentsList
Constructor’s argument list.
-
newTarget
The constructor originally called, in the case of the above example, is P.
The return value
The construct method must return an object.
-
Some non-standard catchers have been scrapped and removed.