Appearance
运行时和编译时 ❤️❤️
设计框架的选择
当我们设计框架的时候 我们有三种选择, 纯运行时, 纯编译时, 编译时 + 运行时
比如我们设计了一个框架 它提供了一个 render 函数 用户提供数据对象 然后使用 Render 函数递归 将数据渲染成 dom 元素 规定的数据结构如下
ts
const obj = {
tag: 'div',
children: [
tag: 'span', children: 'hello erkelost'
]
}
function Render(obj, root) {
const el = document.createElement(obj.tag)
if (typeof obj.children === 'string') {
const text = document.createTextNode(obj.children)
el.appendChild(text)
} else if (obj.children) {
obj.children.forEach(item => Render(item, el))
}
root.appendChild(el)
}const obj = {
tag: 'div',
children: [
tag: 'span', children: 'hello erkelost'
]
}
function Render(obj, root) {
const el = document.createElement(obj.tag)
if (typeof obj.children === 'string') {
const text = document.createTextNode(obj.children)
el.appendChild(text)
} else if (obj.children) {
obj.children.forEach(item => Render(item, el))
}
root.appendChild(el)
}如果有了这个函数 我们就可以这么使用他
ts
const obj = {
tag: 'div',
children: [{ tag: 'span', children: '666' }]
}
Render(obj, document.body)const obj = {
tag: 'div',
children: [{ tag: 'span', children: '666' }]
}
Render(obj, document.body)