4 ms·
I agree, except with the “simply” part. This is not simple at all, unless you also demand that every property can only ever be a string. Otherwise, you need to
by skrebbel 3y ago
I agree, except with the “simply” part. This is not simple at all, unless you also demand that every property can only ever be a string. Otherwise, you need to think of conversions (eg from and to numbers), and what about properties that can be set to objects? Or properies that don’t correspond to attributes at all?
I agree that any sort of convention/rule here that’d be 100% fixed would’ve been better, but it wouldn’t be simple.
- londons_explore 3y agothink the other way round... allow DOM attribute values to be of any javascript type... Sure, they maybe can't be serialized, but that isn't an issue.
- strix_varius 3y agoThat doesn't make any sense. DOM attributes are serialized as a fundamental part of their expression.
- layer8 3y agoSo is JavaScript. Granted, it would be more straightforward if it were a Lisp.
- afavour 3y agoIn what way is that not an issue? What happens when you use .outerHTML to get a string representation of the element? Not to mention that, unavoidably, the entire page HTML arrives to the browser as a string.
- londons_explore 3y agoSame is true of real javascript objects vs JSON. For example, you can't put Date() in JSON. Nor objects with a prototype.
- afavour 3y agoRight… and that’s an issue people have to work around every day. Non string values in HTML world similarly be an issue everyone would have to work around.
- no_wizard 3y agoI do want to point out for those who don't know, on objects with a prototype (such as a class) you can control how JSON.stringify works by adding a special method `toJSON`[0] which will control how that object is serialized. [0]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#:~:text=If%20the%20value%20has%20a%20toJSON()%20method%2C%20it%27s%20responsible%20to%20define%20what%20data%20will%20be%20serialized https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...