4 ms·
Why would you ever want to write data on an element as a property instead of an attribute? Yikes.
by Repulsion9513 2y ago
Why would you ever want to write data on an element as a property instead of an attribute? Yikes.
- deleted 2y ago[deleted]
- kcrwfrd_ 2y agoWhen you don’t want it coerced into a string?
- berkes 2y agoFor primitive types, serde isn't hard. It could even be part of a simple spec. Numbers, Dates, String[], Number[], Date[]. Edit: Or it could just be JSON. For complex types it is. But why and when do you need to store complex types in the DOM? Isn't that always a bad idea? I'm a seasoned web dev, but haven't been working on complex frontend apps, because I believe complexity and frontend web don't go together. So I may very well miss some use-cases.
- bryancoxwell 2y agoFor custom elements you may want to store data or methods that modify the element’s behavior.
- berkes 2y agoMethods as in callbacks? So, code? Do you then store a closure or such? Or a pointer to one (i.e. the function/name)? And how or what data would modify behaviour? As said: I'm unfamiliar with these concepts as I actively try to avoid them :)
- rado 2y agoWhen there is no need to appear in the HTML
- jraph 2y agoYour sentence can be understood in two ways, I'll answer the question "Why use domElement.attrname instead of the corresponding domElement.setAttribute('attrname') and domElement.getAttribute('attrname')?". It can look cleaner (matter of taste). It looks like a native JS assignment, and it's shorter. For open and hidden, it's way more intuitive and convenient to set and get booleans than testing the existence of the corresponding HTML attribute (Still not a fan of this thing, years after having learned this). (but maybe you meant "Why use domElement.randomprop instead of something like domElement.dataset.randomprop"?)
- sir_pepe 2y agoWhen you want to pass an object to a web component without going through JSON or some other stringification procedure.
- politelemon 2y agoHow do you read it in the web component afterwards? Passing it via property sounds quite useful.
- throwitaway1123 2y agoWeb components are implemented using custom elements which are just plain JS classes that extend the HTMLElement class, so you would access the property the same way you would access a property in a normal JS class.
- jaffathecake 2y agoThe author of the thread is arguing against properties.
- throwitaway1123 2y agoYes, I know. I wasn't making a prescriptive comment about whether or not developers should use properties, I was just making a descriptive comment about how properties are accessed in a web component to answer the specific question politelemon asked.
- politelemon 2y agoThanks understood, that makes it quite simple and intuitive thinking about it.
- jaffathecake 2y agoEvent listeners? Image data?
- 2y ago
- shunia_huang 2y agoI believe it's quite useful for framework developers for not required to build an abstract layer upon the already-exist-and-well-designed DOM layer to manage the state transitions or event handlings or something similar, you can't do too much if you are constrained to use only string values to encode/decode the inner state/data on the DOM.