当前位置:网站首页>Typescript syntax
Typescript syntax
2022-06-24 21:06:00 【AcTarjan】
data type
any Any data type let x : any = "nihao"; x = 12;
number Double precision 64 Bit floating point value 0b1011 0o744 12 0xa1 3.14
string character string , Backquotes support inline expressions 'nihao' "nihao" `nihao ${
name }`
boolean Boolean value false true
Array let arr: Array<number> = [1, 2]; let arr : number[] = [1,2];
Tuples let x : [string,number] = ["AcTarjan",21];
enum enumeration enum Color {
Red,Blue,White}; let c : Color = Color.Red
null Missing object value
undefined Default value of uninitialized variable
never Other types ( Include null and undefined) Subtypes of , Represents a value that never appears
Variable
- Naming specification : It can contain numbers 、 Letter 、_ or $; Cannot start with a number
- var and let Can be used to declare variables ,var The scope of is the function scope ,let The scope of is a block level scope , priority of use let
// Variable declarations
let [ Variable name ] : [ Variable type ] = value ;
let [ Variable name ] : [ Variable type ]; // The value is undefined
let [ Variable name ] = value ; // Variables can be of any type
let [ Variable name ]; // Variables can be of any type The value is undefined
// Constant declaration
const [ Constant names ] : [ constant type ] = value ;
const [ Constant names ] = value ; // Constants are of any type
// Variable type assertion ( Similar to type conversion )
< type > value
value as type
let num : number = <number>'12'
let num : number = '12' as number
Process control
// Conditional statements
if () {
}
if () {
} else {
}
if () {
} else if () {
}
switch (name) {
case 'AcTarjan' :
console.log('AcTarjan');
break; /* Optional */
case 'Bob':
console.log('Bob');
break; /* Optional */
default : /* Optional */
console.log('default');
}
// Loop statement
var i : number
for ( i = 0; i < 100; i++ ) {
console.log(i)
}
//for...of Allow you to traverse Arrays( Array ), Strings( character string ), Maps( mapping ), Sets( aggregate ) Such as iterative data structure, etc .
for (let val of list) {
console.log(val)
continue;
}
while () {
break;
}
do {
} while();
function
// General functions
function add(x: number, y: number): number {
return x + y;
}
// Optional parameter function
function buildName(firstName: string, lastName?: string) {
if (lastName)
return firstName + " " + lastName;
else
return firstName;
}
// Parameter default function
function calculate_discount(price:number,rate:number = 0.50) {
var discount = price * rate;
console.log(" The result of the calculation is : ",discount);
}
// Residual parameter function
function buildName(firstName: string, ...restOfName: string[]) {
return firstName + " " + restOfName.join(" ");
}
// Anonymous functions
var msg = function() {
return "hello world";
}
console.log(msg())
//Lambda function
var foo = (x :number) => {
return 10 + x
}
console.log(foo(100)) // The output is 110
Classes and objects
// Class inheritance keyword : extends
// Interface implementation keywords : implements
class Car {
// Field
engine :string;
static num :number;
// Constructors
constructor(engine :string) {
this.engine = engine
}
// Method
disp():void {
console.log(" The engine is : "+this.engine)
}
}
var obj = new Car("Engine 1");
var x = obj instanceof Car // x = true Judge obj Whether it is Car The object of
interface ILoan {
interest :number
}
边栏推荐
- C language to realize mine sweeping (simple version)
- Geek University cloud native training camp
- IDEA Dashboard
- Camera rental management system based on qt+mysql
- The AI for emotion recognition was "harbouring evil intentions", and Microsoft decided to block it!
- JMeter response assertion
- 得物多活架构设计之路由服务设计
- How to enhance influence
- 微信小程序中使用vant组件
- Visitor model -- generation gap between young and middle-aged people
猜你喜欢

图的基本概念以及相关定义

Record a deletion bash_ Profile file

The latest simulated question bank and answers of the eight members (Electrical constructors) of Sichuan architecture in 2022

The Google File System (GFS) learning notes
![[performance tuning basics] performance tuning strategy](/img/83/be41a6a0c5c186d3fb3a120043c53f.jpg)
[performance tuning basics] performance tuning strategy

Internet of things? Come and see Arduino on the cloud

More than ten years' work experience is recommended at the bottom of the box: how much does it cost to find a job? See here! Brothers and sisters are recommended to collect and pay attention

Set up your own website (14)

Camera rental management system based on qt+mysql

Learn to use a new technology quickly
随机推荐
[performance tuning basics] performance tuning standards
伯克利、MIT、劍橋、DeepMind等業內大佬線上講座:邁向安全可靠可控的AI
刚购买了一个MYSQL数据库,提示已有实例,控制台登录实例要提供数据库账号,我如何知道数据库账号。
Undo log and redo log must be clear this time
Learn together and make progress together. Welcome to exchange
opds sql组件能不能将流程参数通过上下文传给下一个组件
C语言实现扫雷(简易版)
Berkeley, MIT, Cambridge, deepmind and other industry leaders' online lectures: towards safe, reliable and controllable AI
Sequence stack version 1.0
JMeter installation plug-in, adding [email protected] -Perfmon metric collector listener steps
Combination mode -- stock speculation has been cut into leeks? Come and try this investment strategy!
After idea installs these plug-ins, the code can be written to heaven. My little sister also has to arrange it
A/B测试助力游戏业务增长
Rip/ospf protocol notes sorting
It was Tencent who jumped out of the job with 26k. It really wiped my ass with sandpaper. It gave me a hand
Where is 5g really powerful? What is the difference with 4G?
Pyaudio audio recording
Leetcode (135) - distribute candy
Several common command operations in win system
Dongyuhui is not enough to bring goods to "rescue" live broadcast