我是否需要对此函数使用严格的声明?它还期望用“:”代替“。”。对于window.onload?

时间:2019-01-30 00:58:31

标签: javascript javascript-objects

我需要对这些功能使用严格的声明吗?它还期望用“:”代替“。”。对于window.onload?它还说未定义“ e”,但是我将其用作函数事件。加上不可恢复的语法错误?这只是网站的简单音频播放器。

var audio = new Audio();
var audioPlayer = {
    albumId: 0,
    audioTrack: 0,
    currentFolder: "",
    albums: [
        {   
            artist: "Post Malone",
            album: "Beerbongs & Bentleys",
            folder: "Beerbongs & Bentleys",
            graphic: "Beerbongs & Bentleys/b&bart.png",
            songs: ["Better Now.mp3", "Psycho ft. Ty Dolla $ign.mp3", "Rockstar ft. 21 Savage.mp3"],
            songTitles: ["Better Now", "Psycho ft. Ty Dolla $ign", "Rockstar ft. 21 Savage"]
        }
    ],
    selectAlbum: function(e){
        console.log(audioPlayer.albumId);
        $("#tracks").empty();
        $("h1").text(audioPlayer.albums[audioPlayer.albumId].album);
        $("#track > span").text(audioPlayer.albums[audioPlayer.albumId].songTitles[audioPlayer.audioTrack]);
        $(".album_cover > img").attr("src", "albums/" + audioPlayer.albums[audioPlayer.albumId].graphic);
        $("#status").text("Ready to Play");
        $.each(audioPlayer.albums[audioPlayer.albumId].songTitles, function(index, item){
            $("#tracks").append("<li>" + audioPlayer.albums[audioPlayer.albumId].songTitles[index] + "</li>");
        });
    },
    playAlbum: function(){
        audio.src = "../albums/" + audioPlayer.albums[audioPlayer.albumId].folder + "/" + audioPlayer.albums[audioPlayer.albumId].songs[audioPlayer.audioTrack];
        audio.play();
    },
    changeTrack: function(direction){
        console.log(direction)
        if(direction === "forward" && audioPlayer.audioTrack < audioPlayer.albums[audioPlayer.albumId].songs.length - 1){
            audioPlayer.audioTrack++;
        }
        if(direction === "back" && audioPlayer.audioTrack < audioPlayer.albums[audioPlayer.albumId].songs.length + 1){
            audioPlayer.audioTrack--;
        }
    },

    window.onload = function(){
    audioPlayer.selectAlbum()
    $("#status").text("Playing");
}

    $(document).on("click", "#albums > div > button",
    function(e){
    var album = this.id.split("_");
    audioPlayer.albumId = parseInt(album[1]) - 1;
    audioPlayer.selectAlbum();
});
};

1 个答案:

答案 0 :(得分:0)

否,不需要添加"use strict"(通常只会导致错误;有关更多详细信息,请参见the MDN article)。

由于尝试从对象文字内部设置window.onload,因此出现语法错误。您只需要将};从最后一行移到window.onload之前的那一行即可。