4 ms·
element.replaceWith(replacement) is just an alias for element.parentNode.replaceChild(element, replacement) with some bonus functionality to convert strings to
by Sephr 8y ago
element.replaceWith(replacement) is just an alias for element.parentNode.replaceChild(element, replacement) with some bonus functionality to convert strings to DOM text nodes. parentNode.replaceChild is not exactly a 'hacky' alternative.
Here it is in the form of a basic polyfill that only supports Elements:
Element.prototype.replaceWith = function(replacement) {
if (typeof replacement === "string") {
replacement = this.ownerDocument.createTextNode(replacement);
}
this.parentNode.replaceChild(this, replacement);
}
- interlocutor 8y agoIs that just as readable? Is the intent just as clear? Is the improved readability of the jQuery version worth the extra 80kb download to you? EDIT: Regarding the polyfill, now you are beginning to write your own little jQuery clone.
- Sephr 8y agoGitHub says they are using polyfills instead of jQuery as well. The point is to not have to rely on external frameworks.
- balfirevic 8y ago> The point is to not have to rely on external frameworks. Why is that a worthwhile goal?
- mschuster91 8y agoTo reduce attack surface as well as maintenance burden for compatibility whenever something breaks during an upgrade. A polyfill is something that's not needed at all in modern browsers and thus can be considered pretty much final. jQuery is actually surprisingly stable compared to Angular/React/npm/rest-of-the-hipster-crap, but it has had its fair share of incompatibilities too.
- Spivak 8y agoIs a set of polyfills philosophically different than a framework?