题解 | #虚拟DOM#
虚拟DOM
http://www.nowcoder.com/practice/a283d2c284ed49468c3b0f6ec0312b0f
考察递归,解构,节点与文本节点,属性设置
const _createElm = vnode => {
// 补全代码
//解构
let {tag,props,children,text}=vnode
function setArr(el,attrs){
for (var key in attrs){
el.setAttribute(key,attrs[key])
}
}
if(typeof tag == 'string'){
vnode.el=document.createElement(tag)
setArr(vnode.el,props)
vnode.el.appendChild(document.createTextNode(text))
children.forEach((child)=>{
vnode.el.appendChild(_createElm(child))
})
}else{
vnode.el=document.createTextNode(text)
}
return vnode.el
}