打字稿 - 显示下划线参数 - 这里有什么问题



我正在尝试用typescript的类方法。 它对我有用。 但是我在"歌曲"的构造中被强调,任何人都向我解释问题是什么?

我不只是纠正我的代码,而是在这里寻找正确的理解。

这是我的代码:

class Song {
artist:string, title:string - both underlined
    constructor( artist:string, title:string ) {}
    play() {
        console.log( 'Playing' + this.title + ' by' + this.artist );
    }
}
var songs = [
    new Song('Bushbaby', 'Megaphone'),
    new Song('Delays', 'One More Lie In'),
    new Song('Goober Gun', 'Stereo'),
    new Song('Sohnee', 'Shatter'),
    new Song('Get Amped', 'Celebrity')
];
class Jukebox {
    constructor(private songs: Song[]) { };
    run () {
        var song = this.getRandomSong();
        song.play();
    };
    private getRandomSong () {
        var songCount = this.songs.length;
        var songIndex = Math.floor(Math.random() * songCount);
        return this.songs[songIndex];
    };
}

var jukebox = new Jukebox(songs);
jukebox.run(); 

只要使用一些分号,我认为你很好。

artist:string;
title:string;

实时更新

相关内容

最新更新