namespace Home {
class Header {
constructor() {
const elem = document.createElement('div');
elem.innerText = "this is header";
document.body.appendChild(elem);
}
}
class Content {
constructor() {
const elem = document.createElement('div');
elem.innerText = "this is content";
document.body.appendChild(elem);
}
}
class Footer {
constructor() {
const elem = document.createElement('div');
elem.innerText = "this is footer";
document.body.appendChild(elem);
}
}
export class Page {
constructor() {
new Header();
new Content();
new Footer();
}
}
}
new Home.Page();
// ts编译后会有4个全局变量 Header、Content、Footer、Page
// 加上命名空间后,只有一个 Home 全局变量