静态方法 Reflect.defineProperty() 基本等同于 Boolean 值。
语法
Reflect.defineProperty(target, propertyKey, attributes)
参数
-
target - 目标对象。
-
propertyKey - 要定义或修改的属性的名称。
-
attributes - 要定义或修改的属性的描述。
返回值
Boolean 值指示了属性是否被成功定义。
异常
如果target不是 TypeError。
描述
Reflect.defineProperty 方法允许精确添加或修改对象上的属性。更多的细节请参阅 Boolean ,来说明该属性是否被成功定义。
示例
使用 Reflect.defineProperty()
const student = {};
Reflect.defineProperty(student, "name", {value: "Mike"}); // true
student.name; // "Mike"
检查属性是否被成功定义
if (Reflect.defineProperty(target, property, attributes)) {
// 成功
} else {
// 失败
}
规范
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 2015 (6th Edition, ECMA-262) Reflect.defineProperty |
Standard | Initial definition. |
| ECMAScript Latest Draft (ECMA-262) Reflect.defineProperty |
Draft |
浏览器兼容性
We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains.
Find out how you can help!
| Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|---|
| Basic support | 49 | 12 | 42 (42) | 未实现 | 未实现 | 10 |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | 未实现 | 49 | 42.0 (42) | 未实现 | 未实现 | 10 |