javascripthtmldatasetconfigurabledefineproperty

Using Object.defineProperty on elem.dataset, but property is still changable


I am trying to set an unchangeable property on the dataset of a canvas element using Object.defineProperty. On a normal object this works fine. But when I try to do it on a dataset object, the property is still changeable.

<canvas id="can" style="outline:1px solid black"></canvas>

<script>
var can = document.getElementById("can");
var obj = {};
Object.defineProperty(can.dataset,"id",{value:2,configurable:false});
Object.defineProperty(obj,"id",{value:4,configurable:false});

can.dataset.id = 55;
console.log(can.dataset.id) //Returns "55" as a string

obj.id = 55;
console.log(obj.id) //Returns 4 - unchanged

</script>

Using writable: false also does not solve it. Is there a way to solve this, is it a bug, am I not supposed to use Object.defineProperty in this way?


Solution

  • Host-provided objects are not required to support the full set of object features defined by JavaScript. Moreover, dataset is specified as a DOMStringMap with well-defined semantics. What you're trying to do would make it not follow those semantics. If it worked, arguably that would be a bug; not working is the host maintaining the defined behavior of the object.