Why is my object returning undefined?

in my ready documentation, I created a videoList object and when I inspect and view the code, the object does exist for a short time. However, shortly after it is created, it returns as not defined by another function. My code looks like this:

$(document).ready(function(){
    var videoList = new Object();
    videoList.videoPlaylist = [];
    videoList.addNew = function(data){
        $('.videoPlayer iframe').first().remove();
    }
    videoList.removeOld = function(){

    }

});

      

This sits in a file called video.js. In another buffer.js file that is loaded after the .js video and is not included in the finished function, I:

function loadTracks(playlistID){
    if(GoogleAuth.currentUser.get().hasGrantedScopes(SCOPE)){
         var request = gapi.client.request({
             'method':'GET',
             'path':'/youtube/v3/playlistItems',
             'params':{
                'playlistId':playlistID,
                'part':'snippet',
                'maxResults':'15',
                'key':'AIzaSyD0OY6xhl9gP9CmPXvU-rN-purRDaTrip8'
             }
         });
        request.execute(function(response){
            $('.playlistContainer').html('');
            response.items.forEach(function(element){
                $('.playlistContainer').append('<div class="songTrack"><img src="'+element.snippet.thumbnails.medium.url+'"/><span data-videoId = "'+element.snippet.resourceId.videoId+'">'+element.snippet.title+'</span></div>');
            });

            $('.songTrack').click(function(){
                var data = $(this).data('videoid');
                videoList.addNew(data);
                console.log('I was clicked');
            });
            console.log(response);
        });
    }
}

      

The bottom line is that it adds a list of videos to the container, and each of them must be clickable. The videoList object will eventually contain a large list of videos, and addNew and removeOld will handle the contents of the arrays. I'm still looking into objects, but from what I'm collecting, shouldn't the videoList object be in the global scope and available to the function loadTracks()

?

+3


source to share


1 answer


I think the problem is that you are declaring a variable inside the scope of a jQuery function, not globally outside of the document. Try something like:



var videoList = new Object();
$(document).ready(function(){
   ...
});

      

+5


source







All Articles