Create a Priority Queue Class. **Warning Spoiler** this.collection?

Create a Priority Queue Class. **Warning Spoiler** this.collection?
0

#1

Tell us what’s happening:
Hi friends,

I just had a question while I was going through this challenge. The variable collection is defined with this keyword which means it’s exposing itself to the public. Shouldn’t this be declared let keyword so that no one can easily manipulate this queue? because the functions inside this class are all declared with this as keywords.

I’ve actually switched this.collection to let collection and I was still able to pass the challenge even though I was only supposed to “Only change code below this line”.

Your code so far

Summary

function PriorityQueue () {
  let collection = [];
  this.printCollection = function() {
    console.log(this.collection);
  };
  // Only change code below this line
  this.enqueue = function(el) {
    collection.push(el);
    collection.sort((a, b) => a[1] - b[1]);
  };
  
  this.dequeue = function(){
    let dequeued = collection.shift();
    return dequeued[0];
  }
  
  this.front = function() {
    return collection[0];
  }
  
  this.size = function() {
    return collection.length;
  }
  
  this.isEmpty = function() {
    return collection.length <= 0;
  }
  // Only change code above this line
}


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/coding-interview-prep/data-structures/create-a-priority-queue-class/


#2

Ithink you are correct. It is better to make it a private scoped variable rather than use this.