Reflect.deleteProperty()

Reflect.deleteProperty() 静态方法类似于 delete 运算符,但作为一个函数。它从对象中删除属性。

¥The Reflect.deleteProperty() static method is like the delete operator, but as a function. It deletes a property from an object.

Try it

语法

¥Syntax

js
Reflect.deleteProperty(target, propertyKey)

参数

¥Parameters

target

要删除其属性的目标对象。

propertyKey

要删除的属性的名称。

返回值

¥Return value

一个布尔值,指示该属性是否已成功删除。

¥A boolean indicating whether or not the property was successfully deleted.

例外情况

¥Exceptions

TypeError

如果 target 不是对象,则抛出该异常。

描述

¥Description

Reflect.deleteProperty() 提供 delete 运算符的反射语义。也就是说,Reflect.deleteProperty(target, propertyKey) 在语义上等同于:

¥Reflect.deleteProperty() provides the reflective semantic of the delete operator. That is, Reflect.deleteProperty(target, propertyKey) is semantically equivalent to:

js
delete target.propertyKey;

在非常低的级别,删除属性会返回一个布尔值(与 代理处理程序 的情况一样)。Reflect.deleteProperty() 直接返回状态,而 delete 如果状态是 false,则会在 严格模式 中抛出 TypeError。在非严格模式下,deleteReflect.deleteProperty() 具有相同的行为。

¥At the very low level, deleting a property returns a boolean (as is the case with the proxy handler). Reflect.deleteProperty() directly returns the status, while delete would throw a TypeError in strict mode if the status is false. In non-strict mode, delete and Reflect.deleteProperty() have the same behavior.

Reflect.deleteProperty() 调用 target[[Delete]] 对象内部方法

¥Reflect.deleteProperty() invokes the [[Delete]] object internal method of target.

示例

¥Examples

使用 Reflect.deleteProperty()

¥Using Reflect.deleteProperty()

js
const obj = { x: 1, y: 2 };
Reflect.deleteProperty(obj, "x"); // true
console.log(obj); // { y: 2 }

const arr = [1, 2, 3, 4, 5];
Reflect.deleteProperty(arr, "3"); // true
console.log(arr); // [1, 2, 3, undefined, 5]

// Returns true if no such property exists
Reflect.deleteProperty({}, "foo"); // true

// Returns false if a property is unconfigurable
Reflect.deleteProperty(Object.freeze({ foo: 1 }), "foo"); // false

规范

Specification
ECMAScript Language Specification
# sec-reflect.deleteproperty

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看