Skip to content

Instantly share code, notes, and snippets.

@dead-claudia
Created February 22, 2016 03:30
Show Gist options
  • Save dead-claudia/8bb1761ec6b0f8cf0dd5 to your computer and use it in GitHub Desktop.
Save dead-claudia/8bb1761ec6b0f8cf0dd5 to your computer and use it in GitHub Desktop.
VDOM-like DOM tree construction
function n(type, attrs, children) {
var elem = document.createElement(type)
for (var attr in attrs) {
if ({}.hasOwnProperty.call(attrs, attr)) {
elem[attr] = attrs[attr]
}
}
(function append(e) {
if (Array.isArray(e)) {
e.forEach(append)
} else if (e != null) {
elem.appendChild(
typeof e === "string" ? document.createTextNode(e) : e)
}
})(children)
return elem
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment