Skip to content Skip to sidebar Skip to footer

Object.defineproperty: Setters For Dom Elements Properties

I can't fully understand how Object.defineProperty works on dom elements. On normal javascript objects it works like a charm var obj={name: 'john'}; Object.defineProperty(obj, 'nam

Solution 1:

That's because you've never set the value in your setter. When you create a setter, you're taking over the job of handling setting the value on the object (on the element, in your case). (And if you try to set it in your setter, e.g. box.checked = newValue, you'll just go into a loop that will only end because of a stack overflow.)

Looking at your code, you're trying to get notification when the checked property of a DOM element is changed. You can't do that with defineProperty. Host objects (like DOM elements) don't have to support defineProperty at all, and even if they do, they don't have to call the setter when the underlying state of the control changes internally.

Post a Comment for "Object.defineproperty: Setters For Dom Elements Properties"