Javascript 简明教程
JavaScript - The Reflect Object
JavaScript Reflect
在 JavaScript 中, Reflect 是一个全局对象,并且在 ECMAScript 6 (ES6) 引入。它包含静态方法,提供了在运行时与对象进行交互的更好方法。
In JavaScript, Reflect is a global object and was introduced in ECMAScript 6 (ES6). It contains static methods, providing a better way to interact with the object at runtime.
与大多数全局对象不同,Reflect 不是一个构造函数。不能将它与 new 运算符一起使用或将 Reflect 对象调用为一个函数。Reflect 的所有方法都是静态的,就像 Math 对象一样。
Unlike most global objects, Reflect is not a constructor. You cannot use it with the new operator or invoke the Reflect object as a function. All methods of Reflect are static just like the Math object.
它包含各种方法,供在运行时访问、更新、删除等对象属性。
It contains various methods to access, update, delete, etc. properties from the object at run time.
Key Features of the Reflect Object
-
Object construction − The Reflect API allows to create objects at run time using the Reflect.construct() method.
-
Function invocation − The apply() method of the Reflect API is used to invoke a function or object method by passing the context and argument.
-
Object property manipulation − A Reflect API has different methods to set, update, or delete the object properties. Also, it can handle the extension of the object by preventing it.
Syntax
以下是使用 Reflect API 方法的语法 −
Following is the syntax to use the method of the Reflect API −
Reflect.method();
在上述语法中,“方法”是概括。“方法”可以是 Reflect API 的任何方法,如 get、set、apply、construct、has 等。
In the above syntax, the 'method' is the generalization. It can be any method of the Reflect API, such as get, set, apply, construct, has, etc.
Reflect Methods
在下面的表格中,我们列出了 Reflect 的所有方法 −
In the following table, we have listed all the methods of Reflect −
Sr.No. |
Name & Description |
1 |
apply() To invoke a function. |
2 |
construct() Allows to specify the different prototypes. |
3 |
defineProperty() Allows to insert or edit object property. |
4 |
deleteProperty() Allows to delete the object property. |
5 |
get() To get a value of the property. |
6 |
getOwnPropertyDescriptor() Allows to get the descriptor of the object. |
7 |
getPrototypeOf() To get the prototype of the object. |
8 |
has() To check whether the property exists in the object. |
9 |
isExtensible() Allows to check whether the object is extensible. |
10 |
ownKeys() Returns the own keys of the targeted object and ignores the inherited keys. |
11 |
preventExtensions() To prevent the extension of the object. |
12 |
set() To set the value to the object property. |
13 |
setPrototypeOf() Allows to set the prototype of the object as null or another object. |
Examples
Example 1
在以下示例中,我们定义了一个汽车对象。'prop' 变量以字符串格式包含属性名称。
In the example below, we have defined a car object. The 'prop' variable contains the property name in the string format.
我们使用 Reflect 对象的 get() 方法从汽车对象中访问 'model' 属性。我们传入对象名称作为 get() 方法的第一个参数,属性名称作为第二个参数。
We use the get() method of the Reflect object to access the 'model' property from the car object. We passed the object name as a first parameter of the get() method and the property name as a second parameter.
在输出中,您可以看到 'model' 属性的值。
In the output, you can see the value of the 'model' property.
<html>
<body>
<div id="output">The model of the car is: </div>
<script>
const car = {
name: "Audi",
model: "Q6",
price: 7000000,
}
let prop = "model";
let model = Reflect.get(car, prop);
document.getElementById("output").innerHTML += model;
</script>
</body>
</html>
The model of the car is: Q6
Example 2
在以下示例中,我们使用 set() 方法将属性设置到对象中。set() 方法将对象、属性名称和值作为参数。
In the example below, we used the set() method to set the property into the object. The set() method takes an object, property name, and value as a parameter.
输出显示了 'doors' 属性值。通过这种方式,您可以使用 Reflect API 的 set() 方法在运行时将属性设置到对象中。
The output shows the 'doors' property value. In this way, you can set the property into the object at run time using the set() method of the Reflect API.
<html>
<body>
<div id="output">The total doors in the Audi Q6 is: </div>
<script>
const car = {
name: "Audi",
model: "Q6",
price: 7000000,
}
const model = Reflect.set(car, "doors", 4);
document.getElementById("output").innerHTML += car.doors;
</script>
</body>
</html>
The total doors in the Audi Q6 is: 4
在执行上面脚本后,输出窗口将弹出,显示网页上的文本。
On executing the above script, the output window will pop up, displaying the text on the webpage.
Example 3
在以下示例中,我们使用 has() 方法动态检查特定属性是否存在于对象中。
In the example below, we used the has() method to check whether the particular property exists in the object dynamically.
这两个符号看起来很相似,但是是不同的,你可以从输出中看到。
Both symbols look similar but are different, which you can see in the output.
<html>
<body>
<p id="output"></p>
<script>
const car = {
name: "Audi",
model: "Q6",
price: 7000000,
}
const isName = Reflect.has(car, "name");
if (isName) {
document.getElementById("output").innerHTML = "The Car object contains the name.";
}
</script>
</body>
</html>
The Car object contains the name.