1. // 使用static关键字修饰的变量,表示类的公用变量,不用实例化直接使用类名调用
  2. //在类中声明的变量 默认是public
  3. class Http{
  4. public static baseUrl:string = "https://www.baidu.com";
  5. static request(){
  6. console.log("request");
  7. }
  8. }
  9. console.log(Http.baseUrl);
  10. Http.request();
  11. var h:Http = new Http();
  12. // 静态变量或静态方法不能通过实例调用
  13. // h.request(); 报错

常量

  1. const a = 10;

类中定义一个常量

  1. //在类中定义一个常量
  2. //readonly只读属性 不能做修改
  3. class Http{
  4. public static readonly baseUrl = "http://www.baidu.com";
  5. public static str:string = "lisi"
  6. }
  7. Http.str = "dai";
  8. Http.baseUrl = "" //报错 不能修改

readonly

  1. 接口 readonly
  2. interface attrs{
  3. readonly id:number;
  4. name:string;
  5. age?:number;
  6. }
  7. var obj:attrs={
  8. id:1001,
  9. name:"lisi"
  10. }
  11. obj.name = "dai",
  12. console.log(obj);
  13. obj.id = 12;//报错