First, let’s take a look at the comments. Comments are actually explanations and descriptions of the code. The purpose is to make it easier for people to understand the code. Adding comments to the code is just to improve the readability of the code, and the comments will not be compiled by the computer.
The comment format may be different in different TypeScript
. The comments in JavaScript
are actually the same as those in 06125c04a0fbb1. Let's take a look.
TypeScript
are divided into single-line comments and multi-line comments:
- single-line comment : the following text is the content of the comment.
// 这是一个单行注释
- Multi-line comment : This comment can span multiple lines.
/*
这是一个多行注释
这是一个多行注释
这是一个多行注释
*/
We will TypeScript
code into JavaScript
time code, comments will be compiled displayed together. When we run the JavaScript
code, the comment code will be ignored and the comment will not be executed.
Example:
For example, add a comment code to the TypeScript
let str :string = "侠课岛";
console.log(str);
// 这是一行注释
Compile and compile it into JavaScript
code through tsc test.ts
command, the test.js
file is as follows:
From the figure, we can see that when TypeScript
compiled into JavaScript
code, the comment will also be compiled successfully.
TypeScript whitespace and line break
TypeScript
language, spaces, tabs and newlines appearing in the program will be ignored. Spaces, tabs, etc. are usually used to indent the code, making the code easy to read and understand.
Example:
In the TypeScript
code below, there is a space in the middle of the two lines of code:
let str:string = "你好侠课岛";
console.log(str);
And after we compiled this code into JavaScript
, the space in the middle line is gone:
TypeScript is case sensitive
Students who have studied JavaScript
JavaScript
is case sensitive. For example, the variable names Age
and age
are different. That is because TypeScript
is a JavaScript
, so we can know that TypeScript
will also be case sensitive.
Because it is case-sensitive, we must pay special attention to coding variables, function names, etc., to prevent errors.
Example:
For example, we define a variable named xkd
, which is named in lowercase letters, and then when we use this variable, we write it as capital letters XKD
:
let xkd:string = "侠课岛";
console.log(XKD);
An error will be reported when compiling the code in VSCode, as shown below:
As you can see from the above figure, when writing code, the VSCode
editor will use a red wavy line to indicate that we are wrong. When the mouse is placed on it, the cause of the error will appear. This is the error message in the VSCode
Function.
If you execute the ts
file in the terminal, the execution result will also tell us where the error was reported and what was the cause of the error.
Naming conventions
- Use
PascalCase
(Pascal nomenclature) to name the type. - Do not use
I
as the interface name prefix. - Use
PascalCase
to name the enumeration value. - Use
camelCase
(camel nomenclature) to name the function. - Use
camelCase
to name attributes or local variables. _
prefix to the private attribute name.- Use complete word spelling as much as possible.
Pascal nomenclature : PascalCase
A method of naming variables in computer programming. Its main feature is to capitalize the first letter of all words that describe the effect of variables, and then connect them directly, without a connector between the words. For example FirstName
, LastName
, Animal
like.
Camel nomenclature : Camel-Case
PascalCase
, except that Camel-Case
just like its name. The first letter of the first word is lowercase, and the first letter of each word after the second word is capitalized. For example firstName
, fastName
, myFirstName
like.
Reserved keywords
TypeScript
are shown in the following table:
break | as | catch | switch |
---|---|---|---|
case | if | throw | else |
var | number | string | get |
module | type | instanceof | typeof |
public | private | enum | export |
finally | for | while | void |
null | super | this | new |
in | return | true | false |
any | extends | static | let |
package | implements | interface | function |
new | try | yield | const |
continue | do |
Reserved keywords refer to words that have been defined in high-level languages. We can no longer use these as variable names or function names.
Example:
For example, use the keyword break
as the variable name:
let break:string = "关键字";
When the code is executed, the following error message will be output:
The difference between TypeScript and JavaScript
TypeScript
can use JavaScript
all the code and programming concepts in, TypeScript
is to make JavaScript
developed and created easier. Here are some differences between TypeScript
and JavaScript
TypeScript
requires a clear type, andJavaScript
does not require it, becauseTypeScript
is a strongly typed language, andJavaScript
is a weakly typed language.TypeScript
provides static type checking at compile time through type annotations.JavaScript
TypeScript
without any modification. At the same time, a compiler can be used to convert theTypeScript
code toJavaScript
.TypeScript
JavaScript
object model from the core language aspect and the molding aspect of the class concept.TypeScript
provides default parameter values.TypeScript
introduces the concept of "class" that is not inJavaScript
TypeScript
introduces the concept of modules, which can encapsulate declarations, data, functions and classes in modules.
Link: https://www.9xkd.com/
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。