Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Unable to delete property' on Safari when trying to delete dataset attribute in strict mode

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?

like image 636
evenfrost Avatar asked Mar 10 '15 20:03

evenfrost


1 Answers

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..

like image 181
Tamer Aktaş Avatar answered Nov 04 '22 02:11

Tamer Aktaş