TypeScript 之构造器 constructor 方法 methods

class Movie {
  name: string;
  play_count: number;
  create_at: string;
  constructor(name: string, play_count: number = 12, create_at: string) {
    // this 指向生成点 Object 本身
    this.name = name;
    this.play_count = play_count;
    this.create_at = create_at;
  }
 
  // methods 可以对 data 进行操作
  display_play_count(padding: string = '***') {
    return this.play_count + '次' + padding    
  }
  increase_play_count() {
    this.play_count += 1;
  }
}

let a = new Movie('阿丽塔:战斗天使', undefined, '17点28分');

a.increase_play_count();  // 13***  虽然第二个参数并没有传递 可以使用 undefined 来占位 会使用默认值 12 再 += 1

console.log(a, a.display_play_count());  // Movie { name: '阿丽塔:战斗天使', play_count: 13, create_at: '17点28分' } '13次***'