HarmonyOS鸿蒙Next中Typescript基础语法

HarmonyOS鸿蒙Next中Typescript基础语法

  1. 基础类型

    • 布尔值

      let isDone: boolean = true
      
    • 数字

      let a: number = 10
      let b: number = 0b1101
      let c: number = 0o376
      let d: number = 0x73e
      
    • 字符串

      let str: string = 'bac'
      let str1: string = "abc"
      
    • 数组

      使用元素类型后面接[]如:number[]
      使用数组泛型Array<元素类型> 如:Array<number>
      
      let arr: number[] = [1,2,3]
      let arr1: Array<number> = [2,3,4]
      
    • 元组

      let a:[string, number] = ['abc',123]
      
    • 枚举

      enum Color{Red,Green,Blue}
      let a:Color = Color.Red
      
    • unknown

      let a:unknown = 4
      a = 'abc'
      a = [1,2,3]
      
    • void

      function abc():void{}
      
    • null和undefined

      let a:undefined = undefined
      let b:null = null
      
    • 联合类型

      let a:string|number = 'abc'
      a = 10
      
  2. 条件语句

    • if…else

      let a:number = 10
      if(a>5){
        ...
      }else if(a>6){
        ...
      }else{
      }
      
    • switch

      let a:string = "A"
      switch(a){
        case "A":{
          ...
          break; 
        }
        case "B":{
          ...
          break; 
        }   
        default:{
          ...
          break;
        }
      }
      
  3. 函数

    • 定义

      // 有名函数
      function add(x, y) {
        return x + y;
      }
      
      // 匿名函数
      let myAdd = function (x, y) {
        return x + y;
      };
      
      // 有名函数:给变量设置为number类型
      function add(x: number, y: number): number {
        return x + y;
      }
      
      // 匿名函数:给变量设置为number类型
      let myAdd = function (x: number, y: number): number {
        return x + y;
      };
      
      // 箭头函数
      ( [param1, parma2,…param n] )=> {
        // 代码块
      }
      
      let arrowFun = ([param1, parma2,…param n])=>{
        // 代码块
      }
      arrowFun(param1, parma2,…param n)
      
    • 类的定义

      class Person {
        private name: string
        private age: number
      
        constructor(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();
      
    • 继承

      class Employee extends Person {
        private department: string
      
        constructor(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();
      
  4. 模块

    • 导出

      export class NewsData {
        title: string;
        content: string;
        imagesUrl: Array<NewsFile>;
        source: string;
      
        constructor(title: string, content: string, imagesUrl: Array<NewsFile>, source: string) {
          this.title = title;
          this.content = content;
          this.imagesUrl = imagesUrl;
          this.source = source;
        }
      }
      
    • 导入

      import { NewsData } from '../common/bean/NewsData';
      
  5. 迭代器

    • for…of

      let someArray = [1, "string", false];
      
      for (let entry of someArray) {
          console.log(entry); // 1, "string", false
      }
      
    • for…in语句

      let list = [4, 5, 6];
      
      for (let i in list) {
          console.log(i); // "0", "1", "2",
      }
      
      for (let i of list) {
          console.log(i); // "4", "5", "6"
      }
      

更多关于HarmonyOS鸿蒙Next中Typescript基础语法的实战教程也可以访问 https://www.itying.com/category-93-b0.html

回到顶部