如何识别javascript不可删除的属性?

How to identify javascript undeletable properties?

在Javascript严格模式下

Deleting an undeletable property is not allowed

为了确保不删除这样一个不可删除的属性,如何确定属性 X 是可删除的并且属性 Y不可删除

背后的概念是……?

The concept behind this is...?

Property attributes。每个 configurable 属性设置为 false 的 属性 不能是 deleted(在草率模式下静默失败并在严格模式下抛出)。

How to figure out whether a property is deletable?

您可以使用 Object.getOwnPropertyDescriptor() function 将属性作为对象访问:

var isDeletable = Object.getOwnPropertyDescriptor(obj, "propName").configurable;

请注意,这仅适用于 obj 的自有属性,不适用于继承的属性;对于那些你必须在各自的原型上调用函数。