Let's talk about Proxy and Reflect in Javascript
This article brings you relevant knowledge about Proxy and Reflect in JavaScript. I hope it will be helpful to you.
ECMAScript has added two new features, Proxy and Reflect, to the ES6 specification. These two new features enhance the controllability of object access in JavaScript, making JS modules, Class encapsulation can be more rigorous and simple, and it can also make error reporting when operating objects more controllable.
Proxy
Proxy, as its name suggests, proxy. This interface can create a proxy object for the specified object. Any operations on the proxy object, such as accessing properties, assigning values to properties, and function calls, will be intercepted and then handed over to the functions we define to handle the corresponding operations.
The characteristics of JavaScript allow objects to have a lot of room for manipulation. At the same time, JavaScript also provides many methods for us to transform objects. We can add attributes at will, delete attributes at will, and change the prototype of the object at will... But the API previously provided by the Object class has Many disadvantages:
- If you want to use Object.defineProperty to define all properties in a certain name collection, you can only set getters and setters for all properties through enumeration, and since you can only set getters and setters for each property Creating a function with a collection that is too large can cause performance issues.
- Object.defineProperty After defining the property, if you still want to have normal access functions, you can only store the data in another property name of the object or need another object to store the data. For those who just want to monitor Properties are particularly inconvenient.
- Object.defineProperty cannot modify non-redefinable properties in a class, such as the length property of an array.
- For properties that don’t yet exist and whose names are difficult to predict, Object.defineProperty can’t help.
- Certain behaviors cannot be modified or prevented, such as: enumerating property names and modifying object prototypes.
The emergence of the Proxy interface solves these problems very well:
- The Proxy interface classifies all operations on the object into several categories, and provides them through the Proxy Traps intercept specific operations, and then perform logical judgments in the processing functions we define to implement complex functions and control more behaviors than before.
- The proxy object created by Proxy exists in the form of a middleman and is not responsible for storing data. We only need to provide the proxy object to external users so that external users can access our original objects under the control of the proxy object. That’s it.
The Proxy interface is a constructor in the JS environment:
1 |
|
This constructor has two parameters. The first one is the object we want to proxy, and the second one contains Objects of functions that handle various operations.
The following is a calling example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
In the above example, an object is first created and assigned to target, and then a proxy object is created with target as the target and assigned to proxy. In the object provided to the Proxy constructor as the second parameter, there is an attribute named "get", which is a function. "get" is the name of a trap in the Proxy interface. Proxy will refer to the attribute we provide as the second parameter. For the attributes in the object, find those attributes with the same attribute name as the trap name, automatically set the corresponding trap and use the function on the attribute as the trap processing function. Traps can intercept specific operations on the proxy object, convert the details of the operation into parameters and pass them to our processing function, allowing the processing function to complete the operation, so that we can control various behaviors of the object through the processing function.
In the above example, the get function provided when constructing the proxy object is the function that handles the operation of accessing the object's properties. The proxy object intercepts the operation of accessing the object's properties and passes the target object and ## to the get function. #The attribute name requested to be accessedTwo parameters, and the return value of the function is used as the result of the access.
Trap names and corresponding function parameters | Intercepted operations | Operation examples | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
get(target, property) |
Access object properties |
target.property or target[property ]
|
||||||||||||||
set(target, property, value, receiver) |
Assignment object property |
target.property = value or target[property] = value
|
||||||||||||||
##has(target, property)
| property in target
|
|||||||||||||||
Determine whether the object can add attributes |
|
|||||||||||||||
Make it impossible to add new properties to the object |
|
|||||||||||||||
Define the properties of the object |
|
|||||||||||||||
Delete the properties of the object |
or delete target[property] or Object.deleteProperty(target, property)
|
|||||||||||||||
Get the object Descriptor of own properties |
Object.getOwnPropertyDescriptor(target, property) |
|
||||||||||||||
Enumerate all the own properties of the object |
Object.getOwnPropertyNames(target). |
concat(Object.getOwnPropertySymbols(target))
|
||||||||||||||
Get the prototype of the object |
Object.getPrototypeOf(target) |
|
||||||||||||||
Set the prototype of the object |
Object.setPrototypeOf(target) |
|
||||||||||||||
Function call |
target(...arguments) | ortarget.apply(target, thisArg, argumentsList)
| ||||||||||||||
Constructor call
|
在上面列出的陷阱里是有拦截函数调用一类操作的,但是只限代理的对象是函数的情况下有效,Proxy 在真正调用我们提供的接管函数前是会进行类型检查的,所以通过代理让普通的对象拥有函数一样的功能这种事就不要想啦。 除了直接 new Proxy 对象外,Proxy 构造函数上还有一个静态函数 revocable,可以构造一个能被销毁的代理对象。
Copy after login 这个静态函数接收和构造函数一样的参数,不过它的返回值和构造函数稍有不同,会返回一个包含代理对象和销毁函数的对象,销毁函数不需要任何参数,我们可以随时调用销毁函数将代理对象和目标对象的代理关系断开。断开代理后,再对代理对象执行任何操作都会抛出 TypeError 错误。
Copy after login 弄清楚了具体的原理后,下面举例一个应用场景。
Copy after login 在上面的例子中,我针对对 secret 属性的访问进行了报错,守护住了“美少女”的秘密,让我们歌颂 Proxy 的伟大!
Copy after login 以下是关于 Proxy 的一些细节问题:
Reflect学过其他语言的人看到 Reflect 这个词可能会首先联想到“反射”这个概念,但 JavaScript 由于语言特性是不需要反射的,所以这里的 Reflect 其实和反射无关,是 JavaScript 给 Proxy 配套的一系列函数。
Copy after login 可以看到,Reflect 上的所有函数都对应一个 Proxy 的陷阱。这些函数接受的参数,返回值的类型,都和 Proxy 上的别无二致,可以说 Reflect 就是 Proxy 拦截的那些操作的原本实现。 那 Reflect 存在的意义是什么呢?
Copy after login 在上面的例子里,由于我很懒,所以我在接管定义属性功能的地方“偷工减料”用了 Reflect 提供的 defineProperty 函数。用 Object.defineProperty 在代理对象上定义 me 属性时报了错,表示失败,而定义 age 属性则成功完成了。可以看到,除了被报错的 me 属性,对其他属性的定义是可以成功完成的。我还使用 Reflect 提供的函数执行了同样的操作,可以看到 Reflect 也无法越过 Proxy 的代理,同时也显示出了 Reflect 和传统方法返回值的区别。 虽然 Reflect 的好处很多,但是它也有一个问题:JS 全局上的 Reflect 对象是可以被修改的,可以替换掉里面的方法,甚至还能把 Reflect 删掉。
Copy after login 基于上面的演示,不难想到,可以通过修改 Reflect 以欺骗的方式越过 Proxy 的代理。所以如果你对安全性有要求,建议在使用 Reflect 时,第一时间将全局上的 Reflect 深度复制到你的闭包作用域并且只使用你的备份,或者将全局上的 Reflect 冻结并锁定引用。 相关推荐:javascript学习教程 |
The above is the detailed content of Let's talk about Proxy and Reflect in Javascript. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
