当前位置:网站首页>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
}
边栏推荐
- Shrimp skin test surface treated
- Bridging mode -- law firm
- C語言實現掃雷(簡易版)
- JMeter response assertion
- Network security review office starts network security review on HowNet
- Image panr
- Does the developer want to change to software testing?
- JMeter installation plug-in, adding [email protected] -Perfmon metric collector listener steps
- Subnet partition operation
- List set Introduction & common methods
猜你喜欢

I feel that I am bald again when I help my children with their homework. I feel pity for my parents all over the world

在Dialog中使用透明的【X】叉叉按钮图片

Agency mode -- Jiangnan leather shoes factory

使用gorm查询数据库时reflect: reflect.flag.mustBeAssignable using unaddressable value

Map跟object 的区别

Combination mode -- stock speculation has been cut into leeks? Come and try this investment strategy!

OSI notes sorting

海泰前沿技术|隐私计算技术在医疗数据保护中的应用

顺序表的基本操作

Static routing job
随机推荐
Background of master data construction
JMeter response assertion
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
Requests requests for web page garbled code resolution
"Ningwang" was sold and bought at the same time, and Hillhouse capital has cashed in billions by "selling high and absorbing low"
A/b test helps the growth of game business
Selenium crawl notes
Dongyuhui is not enough to bring goods to "rescue" live broadcast
After screwing the screws in the factory for two years, I earned more than 10000 yuan a month by "testing" and counterattacked
How to enhance influence
Simulation lottery and probability statistics experiment of the top 16 Champions League
2021-09-30
It was Tencent who jumped out of the job with 26k. It really wiped my ass with sandpaper. It gave me a hand
Leetcode(146)——LRU 缓存
浅谈MySql update会锁定哪些范围的数据
Use the transparent [x] cross button image in the dialog
Bridging mode -- law firm
Static routing job supplement
Vant component used in wechat applet
Open function