zl程序教程

您现在的位置是:首页 >  前端

当前栏目

[TypeScript] instanceof and Type Guards (getPrototypeOf)

typescript and type instanceof
2023-09-14 08:59:13 时间
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);