当前位置:网站首页>Typescript from getting started to mastering (XXII) namespace namespace (I)
Typescript from getting started to mastering (XXII) namespace namespace (I)
2022-07-29 03:45:00 【Haha, APE】
Before our TS The effect is output at the console terminal , Now let's create a new simple project , Let's have ts Running in a project
Build the most basic TS development environment :
- After setting up the folder , open VSCode, Pull the folder into the editor , And then open the terminal , function
npm init -y, establishpackage.jsonfile . - After generating the file , We then run... In the terminal
tsc -init, Generatetsconfig.jsonfile . - newly build
srcandbuildFolder , Build another oneindex.htmlfile . - stay
srcUnder the table of contents , Create a new onepage.tsfile , That's what we're going to writetsThe file . - To configure
tsconfig.jsonfile , Set upoutDirandrootDir, That is, set the file directory to be compiled , And compiled file directories . - Then write
index.html, introduce<script src="./build/page.js"></script>, When let's not yetpage.jsfile . - To write
page.tsfile , Add an outputconsole.log(' Haha, APE '), Input again at the consoletsc, Will generatepage.jsfile - Then go to the browser to view
index.htmlfile , If the F12 You can seeHaha, APE, It shows that our construction is normal .
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="./build/page.js"></script>
<title>Document</title>
</head>
<body></body>
</html>

The above is the environment configuration needed to develop the most basic front-end project ( Working on projects , Use as much as possible TypeScript To write )
Problems when there is no namespace :
For better understanding , First write such code , In the form of class index.html To realize header,content and Footer part , Similar to the template we often say .
stay page.ts In the document , Write the following code :
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);
}
}
class Page {
constructor() {
new Header();
new Content();
new Footer();
}
}
After writing, we use tsc Compile once , And then modify index.html file , Introduce
<body>
<script>new Page();</script>
</body>
The above code seems to have no problem
But you open page.js You will find in the file Variables are all global variables , It's all used var Declarative .
Too many global variables make our project heavy and complex , Code becomes unmaintainable
Our ideal state is just one Page Global variables of , Others can be modularized and encapsulated , It cannot be exposed to the overall situation .
So we can use our namespace
Use of namespaces :
Namespace declaration keywords :namespace
for example : Make a statement namespace Home, Classes that need to be exposed , have access to export key word , In this way, only the exposed classes are global , Others will no longer generate global pollution , The code is as follows :
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();
}
}
}
TS When the code is finished , Until then index.html Make changes in the document , Call in the form of namespace . After finish , use tsc Compile the , You can also use tsc -w Monitoring , As long as there are changes, it will be recompiled .
This is it. TypeScript Provide us with a syntax similar to modular development , Its advantage is to reduce global variables a lot , Realize the basic encapsulation , Reduce the pollution of global variables .
边栏推荐
- 1. Mx6u driver development-2-led driver
- Excel splicing database statement
- 新零售O2O 电商模式解析
- 【redis系列】字符串数据结构
- Functions and comparison of repeaters, hubs, bridges, switches and routers
- 如何判定是stun协议
- The difference between /g /m /i of JS regular expressions
- Li Kou daily question - day 44 -205. Isomorphic string
- Naive Bayes -- continuous data
- Solve the problem of garbled code when opening the project code in idea
猜你喜欢

(nowcoder22529c) diner (inclusion exclusion principle + permutation and combination)

Excel splicing database statement

Instance setup flask service (simple version)

Machine learning based on deepchem

1985-2020 (8 Editions) global surface coverage download and introduction
![MOS tube - rapid recovery application notes (II) [parameters and applications]](/img/54/eb040a51304192def8cfb360c7c213.png)
MOS tube - rapid recovery application notes (II) [parameters and applications]

Simple code implementation of decision tree

Microcomputer principle and interface technology

(codeforce547) c-mike and foam

Cloudera manager platform fault repair record
随机推荐
深入C语言(3)—— C的输入输出流
JS regular expression finds the number of times a character (string) appears (one line of code)
Connect with third-party QQ login
Cannot paste multiple pictures at once
Raft protocol - process demonstration
数字孪生实际应用案例-智慧能源篇
Tristate gate
Since 2019, you must have stopped using this marketing strategy
for_each用法示例
容斥原理
安装抓包证书
Several cases of word wrapping in div
RHCE的at,crontab的基本操作,chrony服务和对称加密和非对称加密
Typescript from entry to mastery (XVIII) joint type and type protection
How to judge stun protocol
最新二开版漫画小说听书三合一完整源码/整合免签接口/搭建教程/带采集接口
RTP 发送 和接收 h265
Why does the 20 bit address bus determine the storage space of 1MB
Simple use of eventbus
Excel拼接数据库语句