If you look at the example of DOMParser from MDN:
var parser = new DOMParser();
var doc = parser.parseFromString(stringContainingXMLSource, "application/xml");
// returns a Document, but not a SVGDocument nor a HTMLDocument
parser = new DOMParser();
doc = parser.parseFromString(stringContainingXMLSource, "image/svg+xml");
// returns a SVGDocument, which also is a Document.
parser = new DOMParser();
doc = parser.parseFromString(stringContainingHTMLSource, "text/html");
// returns a HTMLDocument, which also is a Document.
They keep creating new DOMParser
instances. But why? Wouldn't one parser instance suffice? What about code that does a lot of parsing, is there a performance advantage in creating new instances?
EDIT: People are getting hung up on the example. To phrase my question better:
why isn't DOMParser
more like JSON
and its parse
method? Why isn't parseFromString
a static method?