虚拟DOM转换为真实DOM 初始化与更新时的转换原理
function createrElement(vonde){
let tag = vonde.tag
let attrs = vonde.attrs
let children = vonde.children
if(!tag){
return null
}
let elem=document.createElement(tag)
let attrName
for(attrName in attrs){
if(attrs.hasOwnproperty(attrName)){
elem.setAttribute(attrName,attrs[attrName])
}
}
children.forEach(childVnode => {
elem.appendChild(createrElement(childVnode))
});
return elem
}
function updateChildren(vnode,newVnode){
let children = vnode.children || []
let newChildren = newVnode.children || []
children.forEach((childrenVnode,index)=>{
let newChildrenVnode = newChildren[index]
if(newChildrenVnode.tag===newChildren.tag){
updateChildren(childrenVnode,newChildrenVnode)
}else{
replaceNode(childrenVnode,newChildrenVnode)
}
})
}