4 ms·
The "X" stands for "XML", and these things are all XML: - You need to close tags you wouldn't need to close in HTML (breaks copy+pasted HTML) - Argument qu
by Zarel 8y ago
The "X" stands for "XML", and these things are all XML:
- You need to close tags you wouldn't need to close in HTML (breaks copy+pasted HTML)
- Argument quoting isn't optional (breaks copy+pasted HTML)
- HTML style Comments aren't allowed (breaks copy+pasted HTML)
- Whitespace gets stripped for all elements (breaks PREs)
JSX is based on XML rather than HTML because XML is a version of HTML with all the backwards-compatibility weirdness stripped out, which makes it easier to parse. If you've learned XHTML, JSX isn't very hard.
The stuff about <pre> elements and elements as values are pretty easy if you remember that all attributes are just JavaScript values, and the contents of a JSX element is just the "children" attribute.
In other words, these are the same:
<foo>{bar}</foo>
<foo children={bar} />
And `bar` is just a JavaScript value - a JSX fragment, a string, a number, whatever you want it to be.
- _pmf_ 8y ago3. and 4. are valid in XML.
- Zarel 8y agoOh, huh, I didn't know XML uses the same comments as HTML. 4 is valid, though: XML requires special directives to preserve whitespace. JSX requires different syntax with {`...`}, but it's a similar idea.
- _pmf_ 8y ago> XML requires special directives to preserve whitespace Not in what you would consider content: from http://usingxml.com/Basics/XmlSpace http://usingxml.com/Basics/XmlSpace: "White space in any other location must be passed on to the processing application, according to the XML specification."
- IgorPartola 8y agoI don’t really care how easy it is to parse for a machine. I want it to be easy to parse for me. And HTML5 is plenty easy. GP is right, JSX is a weird bastard child of two languages that is very awkward.
- majewsky 8y ago> I don’t really care how easy it is to parse for a machine. I want it to be easy to parse for me. So instead of writing code, you just want to write a natural-language description of what the program should do?
- enraged_camel 8y agoThis sounds like a very bizarre strawman version of what the parent is saying. No, obviously the desire is not to write code like one writes natural language. But ease of reading is an extremely important quality to have in programming languages. Programmers read code way more often than they write it, and them being able to make sense of it is crucial.
- IgorPartola 8y agoWhat enraged_camel said. Machine code is easy to parse. Yet we don’t optimize our tools for that. Readable code is more important than parsable code in most cases.
- hiccuphippo 8y agoI find it difficult to parse for me when a tag doesn't end. The browser has a very specialized parser that can figure out if a ul after a p is inside it or is the p implicitly closed at that point, but I can't figure that out myself. So I welcome the X in XHTML, it keeps the syntax sane.
- Zarel 8y agoTo add on to hiccuphippo's comment: By "easier to parse", I mean "easier to parse for humans as well as computers". I, a human, would also prefer not to memorize the list of self-closing tags, or the arcane rules for when <p> and <li> autoclose. Not having to do these things _blatantly_ makes XHTML more readable.
- johnisgood 8y agoAgreed. I was just reading about when to close tags and when you shouldn't... well... it seems arbitrary to say the least. I would rather just close ALL tags, no exceptions.