[TypeScript] instanceof and Type Guards (getPrototypeOf)

class Foo {
    bar() {}
}

const bar = new Foo()
console.log(bar instanceof Foo) // true
console.log(Object.getPrototypeOf(bar) === Foo.prototype) // true

 

class Song {
  constructor(public title: string, public duration: number) { }
}

class Playlist {
  constructor(public name: string, public songs: Song[]) { }
}

function getItemName(item: Song | Playlist) {
  if (item instanceof Song) {
    return item.title;
  }
  return item.name;
}

const songName = getItemName(new Song('Wonderful Wonderful', 300000));
console.log('Song name:', songName);

const playlistName = getItemName(
  new Playlist('The Best Songs', [new Song('The Man', 300000)])
);
console.log('Playlist name:', playlistName);

 

posted @ 2020-10-04 01:16  Zhentiw  阅读(103)  评论(0编辑  收藏  举报