|
| 1 | +class Foo { |
| 2 | + bar() {} |
| 3 | +} |
| 4 | + |
| 5 | +const bar = new Foo(); |
| 6 | + |
| 7 | +console.log(bar); |
| 8 | + |
| 9 | +// console.log(bar instanceof Foo); |
| 10 | +// console.log(Object.getPrototypeOf(bar) === Foo.prototype); |
| 11 | + |
1 | 12 | class Song { |
2 | | - constructor(public title: string, public duration: string | number) {} |
| 13 | + constructor(public title: string, public duration: number) {} |
| 14 | +} |
| 15 | + |
| 16 | +class Playlist { |
| 17 | + constructor(public name: string, public songs: Song[]) {} |
3 | 18 | } |
4 | 19 |
|
5 | | -function getSongDuration(item: Song) { |
6 | | - if (typeof item.duration === "string") { |
7 | | - return item.duration; |
| 20 | +function getItemName(item: Song | Playlist) { |
| 21 | + if (item instanceof Song) { |
| 22 | + return item.title; |
8 | 23 | } |
9 | | - const { duration } = item; |
10 | | - const minutes = Math.floor(duration / 60000); |
11 | | - const seconds = (duration / 1000) % 60; |
12 | | - return `${minutes}:${seconds}`; |
| 24 | + return item.name; |
13 | 25 | } |
14 | 26 |
|
15 | | -const songDurationFromString = getSongDuration( |
16 | | - new Song("Wonderful Wonderful", "05:30") |
17 | | -); |
18 | | -console.log(songDurationFromString); |
| 27 | +const songName = getItemName(new Song("wonderful wonderful", 300000)); |
| 28 | +console.log("Song Name :", songName); |
19 | 29 |
|
20 | | -const songDurationFromMS = getSongDuration( |
21 | | - new Song("Wonderful Wonderful", 330000) |
| 30 | +const playlistName = getItemName( |
| 31 | + new Playlist("The Best Songs", [new Song("The Man", 30000)]) |
22 | 32 | ); |
23 | 33 |
|
24 | | -console.log(songDurationFromMS); |
| 34 | +console.log("Playlist Name :", playlistName); |
0 commit comments