arkTS基础
let hi: string = 'hello';
hi = 'hello,world';
const hi: string = 'hello';
let h1: string = 'hello';
let h2 = 'hello';
let n1 = 3.14;
let n2 = 3.14159;
let n3 = .5;
let n4 = 1e2;
function factorial(n: number): number{
if(n<1){
return 1}
return n*factorial(n-1);
}
let isDone: boolean = true
if(isDone){
console.log('Done!')}
let s1 = 'hello world\n';
let s2 = 'this is a string';
let a = 'success';
let s3 = `the result is ${
a}`;
let name:string[] = ['Alice','Bob','Carol'];
enum ColorSet {
Red ,Green ,Blue}
enum ColorSet {
White=0xFF,Grey=0x7F,Black=0x00}
let c: ColorSet = ColorSet.Red;
class Cat {
name:string = 'cat'}
class Dog {
name:string = 'dog'}
type Animal = Cat | Dog | number
let animal:Animal = new Cat();
animal = 42
===
!==
==
!=
if语句
if (condition1){
}
else if (condition2){
}
else (condition3) {
}
let s1 = 'hello'
if (s1) {
console.log(s1)}
Switch语句
switch (expression){
case label1: break;
case label2: break;
default: console.log('default');
}
条件表达式
condition ? expression1 : expression2
let isValid = Math.random()