当前位置:网站首页>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.json
file . - After generating the file , We then run... In the terminal
tsc -init
, Generatetsconfig.json
file . - newly build
src
andbuild
Folder , Build another oneindex.html
file . - stay
src
Under the table of contents , Create a new onepage.ts
file , That's what we're going to writets
The file . - To configure
tsconfig.json
file , Set upoutDir
androotDir
, 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.js
file . - To write
page.ts
file , Add an outputconsole.log(' Haha, APE ')
, Input again at the consoletsc
, Will generatepage.js
file - Then go to the browser to view
index.html
file , 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 .
边栏推荐
- Typescript from entry to mastery (XVIII) joint type and type protection
- What you see and think in Microsoft
- Tencent cloud logs in with PEM
- With more than 5 years of work experience and a salary of 15K, would you accept it if you were me?
- Why don't programmers work blindly?
- Notes on letter symbol marking of papers
- @Configuration (proxybeanmethods = false) what's the use of setting this to false
- Vs code must know and know 20 shortcut keys!
- Spark dataframe replaces empty characters (or other values) in each column with null
- 如何判定是stun协议
猜你喜欢
Use of leak scanning (vulnerability scanning) tool burpsuite or burp Suite (with installation and installation package download of burpsuite+1.7.26)
Shopify seller: EDM marketing should be combined with salesmartly to easily get the conversion rate
向日葵资深产品总监技术分享:“国民远控”如何在AD域环境下应用
实例搭建Flask服务(简易版)
EMD empirical mode decomposition
Uni app internationalization
机器学习【Numpy】
CUB_ Visualization of key points in 200 bird dataset
Deep into C language (1) -- operators and expressions
Machine learning [numpy]
随机推荐
如何判定是stun协议
Code ~ hide or disable the status bar and virtual keys
Simple understanding of CDN, SDN and QoS
How fast does it take to implement a super simple programming language?
Typescript from getting started to mastering (XX) function generics
MOS管 —— 快速复苏应用笔记(贰)[参数与应用]
I.MX6U-驱动开发-2-LED驱动
力扣每日一题-第44天-205. 同构字符串
What have I learned from 200 machine learning tools?
消费行业数字化升级成 “刚需”,weiit 新零售 SaaS 为企业赋能!
3.2 model saving and loading
Why do many programmers hate pair programming?
向日葵远程控制为何采用BGP服务器?自动最优路线、跨运营商高速传输
Common methods of lodash Library
Functions and comparison of repeaters, hubs, bridges, switches and routers
sql
Target detection, industrial defects, image segmentation -- deep learning data set induction
Raft protocol - process demonstration
Flutter 启动白屏
How do programmers use code to completely end those things in the system?