forked from kelly422/Frontend-01-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
173 lines (137 loc) · 3.21 KB
/
main.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
function createElement(comp, attributes, ...children) {
// console.log(arguments);
// // debugger;
let ele;
if (typeof comp === 'string') {
ele = new Wrapper(comp);
} else {
ele = new comp({
initData: {}
});
}
for(let name in attributes) {
// ele[name] = attributes[name]; // attribute 和 property 是同一个
ele.setAttribute(name, attributes[name])
}
for (let child of children) {
if (typeof child === 'string') {
child = new TextNode(child);
}
ele.appendChild(child); // 添加 children 的方法一
// ele.children.push(child) // 方法二
}
return ele;
}
class MyComponent {
constructor(config) {
// console.log('config', config);
this.children = [];
this.root = document.createElement('div');
}
set id(v) { // property
console.log('MyComponent::id', v)
}
setAttribute(name, value) { // attribute
// console.log('MyComponent::setAttribute', name, value);
this.root.setAttribute(name, value)
}
appendChild(child) { // 添加children 的方法一
this.children.push(child)
}
render() {
return(
<article>
<header>I am a header</header>
{this.slot}
<footer>I am a footer</footer>
</article>
)
}
mountTo(parent) {
// parent.appendChild(this.root);
//
// for (let child of this.children) {
// child.mountTo(this.root)
// }
this.slot = <div></div>
for (let child of this.children) {
this.slot.appendChild(child)
}
this.render().mountTo(parent)
}
}
class Wrapper {
constructor(type) {
this.children = [];
this.root = document.createElement(type)
}
setAttribute(name, value) { // attribute
// console.log('MyComponent::setAttribute', name, value);
this.root.setAttribute(name, value)
}
appendChild(child) { // 添加children 的方法一
this.children.push(child)
}
mountTo(parent) {
parent.appendChild(this.root);
for (let child of this.children) {
child.mountTo(this.root)
}
}
}
class TextNode {
constructor(text) {
this.root = document.createTextNode(text)
}
mountTo(parent) {
parent.appendChild(this.root)
}
}
class Child {
constructor(config) {
console.log('config', config);
this.children = [];
this.root = document.createElement('div');
}
set id(v) { // property
console.log('MyComponent::id', v)
}
setAttribute(name, value) { // attribute
console.log('MyComponent::setAttribute', name, value)
}
appendChild(child) {
this.children.push(child)
}
mountTo(parent) {
parent.appendChild(this.root);
for (let child of this.children) {
child.mountTo(this.root)
}
}
}
class Grandson {
}
let component = <MyComponent id='a' class='b' style="width: 100px; height: 100px; background-color: lightgreen">
<div>111</div>
{/*<MyComponent>*/}
{/*{new Wrapper('span')}*/}
{/*/!*<Grandson></Grandson>*!/*/}
{/*</MyComponent>*/}
{/*<MyComponent></MyComponent>*/}
</MyComponent>
;
// component.id = 'c'
// console.log(component)
// component.setAttribute('attr', 'haha')
component.mountTo(document.body)
// jsx 的写法转义成 js 语法
// var component = createElement(
// MyComponent,
// {
// id: "a",
// "class": "b"
// },
// createElement(Child, null),
// createElement(Child, null),
// createElement(Child, null)
// );