Consider following code:
(function () {
'use strict';
delete document.body.dataset.state;
})();
where body dataset
is empty. Safari treats all DOMStringMap values — well, I guess so — as ReadOnly
, and their enumerable
, configurable
and writable
descriptor values are all set to false
. This causes the TypeError: Unable to delete property
to appear in the mentioned above example.
But in Chrome dataset property descriptor values are set to true
(can be checked with Object.getOwnPropertyDescriptor()
), and deleting inexistent attribute does not throw the error.
So, what behavior is correct? The spec says about readonly dataset, but writable DOMStringMap, so I assume dataset properties must be deletable. Am I missing something?
Its probably because of strict mode.
Third, strict mode makes attempts to delete undeletable properties throw (where before the attempt would simply have no effect):
'use strict';
delete Object.prototype; // throws a TypeError
You may want to take a look at this documentation
Also you can try set object value to undefined, its a little hairy but works..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With