-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelement.js
30 lines (27 loc) · 1.03 KB
/
element.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Element {
constructor(type, attr, child) {
this.type = type
this.attrs = attr
this.child = child || []
}
render() {
let virtualDOM = document.createElement(this.type)
// attr是个对象所以要遍历渲染
for (var attr in this.attrs) {
virtualDOM.setAttribute(attr, this.attrs[attr])
}
// 深度遍历child
this.child.forEach(el => {
console.log(el instanceof Element)
//如果子节点是一个元素的话,就调用它的render方法创建子节点的真实DOM,如果是一个字符串的话,创建一个文件节点就可以了
// 判断一个对象是否是某个对象的实力
let childElement = (el instanceof Element) ? el.render() : document.createTextNode(el);
virtualDOM.appendChild(childElement);
});
return virtualDOM
}
}
function createElement(type, attr, child) {
return new Element(type, attr, child)
}
module.exports = { createElement }