1.TypeScript
1.1变量
布尔值let isDone: boolean = false;数字let decLiteral: number = 2023;
let binaryLiteral: number = 0b11111100111;
let octalLiteral: number = 0o3747;
let hexLiteral: number = 0x7e7;
console.log('decLiteral is' + decLiteral)字符串let name: string = "Jacky";4. 数组
let list1: number[] = [1, 2, 3];
let list2: Array<number> = [1, 2, 3];5.元组
let x: [string, number];
x = ['hello', 10]6. 枚举
enum Color {Red, Green, Blue};
let c: Color = Color.Green;7.
let notSure: unknown = 4;
notSure = 'maybe a string instead';
notSure = false;
1.2.函数
1.voidfunction test() void {
console.log('This is function is void');
}2.null 和 undefined
let u: undefined = undefined;
let n: null = null;3.
let myFavoriteNumber: string | number;;
myFavoriteNumber = 'seven';
myFavoriteNumber = 7;4.if语句
let num: number = 5;
if (num > 0)
{console.log('数字是正数');
}let num: number = 12;
if (num % 2 == 0){console.log('数字是偶数');
}
else
{console.log('数字是奇数');
}5.switch语句
var grade: string = 'A';
switch(grade) {case 'A': {console.log('优');break;}case 'B': {console.log('良');break;}case 'C': {console.log('及格');break;}case 'D': {console.log('不及格');break;}default: {console.log('非法输入');break;}
}
// 1.有名函数:给变量设置为number类型
function add(x: number, y: number): number {return x + y;
}// 2.匿名函数:给变量设置为number类型
let myAdd = function (x: number, y: number): number {return x + y;
};//3.可选参数
function buildName(firstName: string, lastName?: string) {if (lastName)return firstName + ' ' + lastName;elsereturn firstName;
}let result1 = buildName('Bob');
let result2 = buildName('Bob', 'Adams'); //4.剩余参数
function getEmployeeName(firstName: string, ...restOfName: string[]) {return firstName + ' ' + restOfName.join(' ');
}let employeeName = getEmployeeName('Joseph', 'Samuel', 'Lucas', 'MacKinzie');//5.箭头函数( [param1, parma2,…param n] )=> {// 代码块
}
let arrowFun = ( [param1, parma2,…param n] )=> {// 代码块
}
arrowFun(param1, parma2,…param n)
1.3.类
class Person {private name: stringprivate age: numberconstructor(name: string, age: number) {this.name = name;this.age = age;}public getPersonInfo(): string {return `My name is ${this.name} and age is ${this.age}`;}
}let person1 = new Person('Jacky', 18);
person1.getPersonInfo();//2.继承class Employee extends Person {private department: stringconstructor(name: string, age: number, department: string) {super(name, age);this.department = department;}public getEmployeeInfo(): string {return this.getPersonInfo() + ` and work in ${this.department}`;}
}let person2 = new Employee('Tom', 28, 'HuaWei');
person2.getPersonInfo();
person2.getEmployeeInfo();
2.ArkTS
2.1自定义组件的组成
@Entry
@Component
struct ToDoList {...}在自定义组件内需要使用build方法来进行UI描述。
@Entry
@Componentstruct ToDoList...build() {...}
}@Entry
@Component
struct ToDoList {...build() {Column(...) {Text(...)...ForEach(...{TodoItem(...)},...)}...}
}
2.2 export 导出的用法
//1.导出变量或常量:
export const myVar = 10;const PI = 3.14;
export { PI };//2.导出函数:
export function myFunction() {console.log('Hello, world!');
}//3.导出类:
export class MyClass {private name: string;constructor(name: string) {this.name = name;}greet() {console.log(`Hello, ${this.name}!`);}
}//4.导出模块:// 在当前文件中定义模块
module MyModule {export const someVar = 'Hello';export function someFunction() {console.log('This is a function in MyModule');}
}// 导出整个模块
export { MyModule };//5.导出默认值export default function() {console.log('This is the default function');
}
import myDefaultFunction from './path-to-file';
myDefaultFunction(); // 调用默认导出的函数//6.组合导出
// 命名导出
export const myVar = 10;
export function myFunction() {console.log('Hello, world!');
}// 默认导出
export default class MyClass {private name: string;constructor(name: string) {this.name = name;}greet() {console.log(`Hello, ${this.name}!`);}
}