0

I am currently develoing a webapp where I have to worry about potentially large arrays. Those arrays contain a number of ids, that will be added to html-lists. So given an array of 170 ids I will end up with a ul, that holds 170 li-elements. Since I don't have to worry about search, but performance I decided to use lazy loading. So far I am loading the big array once and am now writing a function, that adds new element according to the users position.

This function needs to remember its last position, in case it does not get a specific new number (the second parameter beeing set). I tried to realize this with the help of a function attribute. Here is what I have so far (ids being the array, pos the position which can be set):

Player.prototype.showPlaylist = function (ids, pos) {
    //In case pos isn't set, use your old value
    showPlaylist.pos = pos !== "undefined" ? showPlaylist.pos : showPlaylist.pos;
    console.log(showPlaylist.pos);
    for (var i = showPlaylist.pos; i < ids.length; i++) {
        //create new html elements
    }
}

Unfortunately I get a

showPlaylist is not defined

at the console.log(). This is the first time I am working with both lazy loading and a function which has a memory, so there might be a better solution (I really what to avoid global variables though).

stiller_leser
  • 1,512
  • 1
  • 16
  • 39
  • 2
    Your logic doesn't make sense, so if `pos` is defined `showPlaylist.pos` is equal to `showPlaylist.pos` otherwise `showPlaylist.pos` is equal to `showPlaylist.pos`? – elclanrs Feb 23 '14 at 00:24
  • 1
    Also, you need to refer to `this.pos` (or `Player.prototype.showPlaylist.pos`) instead of `showPlaylist.pos`. – Cedric Reichenbach Feb 23 '14 at 00:26
  • @CedricReichenbach Better to use `this.pos`, because `Player.prototype.showPlaylist.pos` will be shared across all the instances of the `Player` – ajukraine Feb 23 '14 at 00:28
  • @elclanrs: I am a bit confused, I obviouly meant to set it to this.pos in case the pos-parameter wasn't passed in, which would be ===, but that wont work either? It is working in the longer if logic, but I don't see my error in the short one. Otherwise, thanks for the correction to "this". – stiller_leser Feb 23 '14 at 00:33

2 Answers2

2
Player.prototype.showPlaylist = function (ids, pos) {
    //In case pos isn't set, use your old value
    this.pos = pos !== "undefined" ? this.pos : pos;
    console.log(this.pos);
    for (var i = 0; i < this.pos.length; i++) {
        //create new html elements
    }
}

Use this keyword for storing position because its object specific.

even if you want to attach pos to showPlayList function then also notion of showPlaylist.pos is wrong because to attach that Player.prototype.showPlaylist.pos should be written.

WebServer
  • 1,316
  • 8
  • 12
1

There could be several things wrong with your code, first the pos member is on this as already mentioned. The value of this could be not the playlist.

More details about prototype and value of this can be found here.

Community
  • 1
  • 1
HMR
  • 37,593
  • 24
  • 91
  • 160