Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify javascript undeletable properties?

In Javascript strict mode

Deleting an undeletable property is not allowed

To make sure that one do not delete such an undeletable property, how do one figure out property X is deletable and property Y is undeletable

The concept behind it is......?

like image 766
xameeramir Avatar asked Dec 18 '15 14:12

xameeramir


1 Answers

The concept behind this is...?

Property attributes. Every property that has its configurable attribute set to false cannot be deleted (which fails silently in sloppy mode and throws in strict mode).

How to figure out whether a property is deletable?

You can use the Object.getOwnPropertyDescriptor() function to access the attributes as an object:

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

Notice that this will only work for own properties of obj, not inherited ones; for those you will have to call the function on the respective prototype.

like image 178
Bergi Avatar answered Sep 27 '22 23:09

Bergi