TypeScript class 便捷设置实例属性

index.ts

class Foo {
  constructor(public name: string, public readonly age: number) {}
}

const foo = new Foo("foo", 23);
console.log(foo.name);
console.log(foo.age);

foo.name = "foofoo";
console.log(foo.name);

// foo.age = 24; // Error

posted on 2021-09-09 10:24  aisowe  阅读(154)  评论(0编辑  收藏  举报

导航