This is the sixth day of my participation in the August More text Challenge. For details, see:August is more challenging
Understand some programming language terminology
identifier
Use a convention to name variables, constants, functions, classes, etc., to establish a mapping between names and entities. This name is called an identifier.
Identifiers usually consist of letters and numbers and a few other unique characters.
The data type
A Data Type is a Data classification that contains a set of common attributes of Data and a general name for methods that tell the compiler or interpreter how to use the Data. A data type defines what a developer can do to the data, what the data consists of, and how values of that type can be stored.
Raw data type
Primitive Data types are usually built-in or basic language implementation types. Raw data types generally correspond one-to-one to objects in the computer’s memory, but may not be consistent due to language and implementation differences. However, it is often the fastest to operate on raw data types. Primitive types are basically value types whose assignments are copies copied in memory.
Variables and parameters
A Variable is a placeholder for a value that can be referenced by the name of the Variable. A variable is usually composed of a variable name, a variable type, and a variable value.
Parameters are the values that a function operates on.
Functions and methods
A function is a piece of code that is called by a function name to provide a service to the outside world.
A method is also a piece of code and is called by the method name, but it must be attached to an object.
The methods and functions are roughly the same in form, but there are differences in use. When you associate a function with an object, a function is a method.
Functions can be called directly by the function name, while methods must be called by object and method names.
Expressions and statements
An Expression is an Expression that consists of numbers, operators, parentheses, and variable names in a certain order and can be evaluated, for example, x +(7*y)+2. An expression is essentially a value and can be used as a concrete value. So you can assign it to a variable or you can pass it as an argument. A single operand (constant or variable) can also be called an expression, which is the simplest expression.
Statements in TypeScript are terminated by semicolons. A Statement is a complete computer instruction, including declarations, assignments, function expressions, null statements, and compound statements (one or more statements surrounded by curly braces {}). The difference is that expressions can be evaluated, but statements cannot.
literal
A Literal is a Notation used to represent a fixed value in coding. Almost all computer programming languages have literal representations of basic values, such as floating point numbers, strings, and Boolean types. Literal quantities are also called direct quantities. For example, “Hello World” is a literal string; 99.88 is a numeric literal, and true is a Boolean literal.
Learn TypeScript’s simple syntax
Annotation syntax
There are three main types of comments in the TypeScript language: single-line comments, multi-line comments, and comments used to generate API documentation. The three annotation methods are as follows:
// When a line comment /* Multi-line comments can be commented across lines */ /** * API documentation comments can be identified by TypeDoc tools to generate API documentation */Copy the code
Case sensitive
TypeScript is case sensitive. The variable name someThing is different from someThing. So be careful when coding
Reserved words
TypeScript has many built-in types, objects, etc., that occupy some identifiers. These special identifiers for the system are reserved words for the language and cannot be used to name variables. For example, the following keywords are reserved and cannot be used as identifiers:
The following keywords have special meaning in a specific context and are legal identifiers, but are not recommended to prevent ambiguity:
Use statements; separated
A semicolon (;) must be used between two statements on the same line To separate. The end of each line can be omitted; This is not recommended, however, because when you compress the code, you compress it to a single line, so two statements that are not separated can cause an error.
The file name extension is.ts
TypeScript script files have.ts extensions.
Variable declarations
TypeScript allows you to declare a variable with let or var. The syntax for declaring a variable is: let or var variable name: data type = initialization value;
let varName : string = "hello world" ;
Copy the code
Exception handling
In TypeScript, you can throw an exception with the throw keyword. In JavaScript, a throw can throw an exception of any type.
But in TypeScript, the throw must throw an Error object, as shown below. Throw new Error(” Error message “); To customize an exception, you can extend the Error class. Custom exceptions are useful when you need a specific exception behavior or when you want the catch block to be able to identify the exception type.
Exception handling requires the use of try… Catch block.