1

Im looking for how to reach the variables inside a constructor without using 'this' keyword. If I put this in front of the variables, everything works fine.

function MyQueue() {
    this.collection = [];
    this.index = 0;
}

MyQueue.prototype.isEmpty = function () {
    return this.collection.length === 0;
}

However, if I remove this keyword, I can't reach collection and index when I create an object. Is there any way to reach these variables?

function MyQueue ()
{
     let collection = [];
     let index = 0;
}


MyQueue.prototype.isEmpty = function()
{
   return this.collection.length === 0; 
}

This one doesn't work. How can I reach the collection and index inside a constructor? Thanks in advance.

Wang Liang
  • 4,244
  • 6
  • 22
  • 45
  • I am not sure if thats possible, as the `this` keyword is require to bind attributes to the function in order for them to be accessible in any prototype you create via the function. variables declared with `let` are not binded to the function, they are only local to it. – Abdulfatai Sep 20 '18 at 19:23
  • no, once (local - e.g. `let`, `var`, `const`) variable value is not assigned to some "outer" variable(`this`, variable from outer scope or global variable) it will be lost after appriate block(or function in case of `var` variable) is finished. But what is your goal? Why do you need that at all? Let's work on goal itself! – skyboyer Sep 20 '18 at 19:25
  • 1
    Why you don't want to use `this`? You want `collection` to be independent from `MyQueue` instance? – ponury-kostek Sep 20 '18 at 19:26
  • Those are properties, not variables, and yes you have to use `this` for them. – Bergi Sep 20 '18 at 19:31
  • Thank you for answers, I'm new at Javascript, therefore just trying to get ideas what really 'this' do. – Melih Bağçeli Sep 20 '18 at 19:33
  • Here's a good article which I believe explains how it can be done by example https://medium.freecodecamp.org/here-are-some-practical-javascript-objects-that-have-encapsulation-fc4c1a79c655 . There's even a queue implementation, with proper private vars and public accessors. – Egor Stambakio Sep 20 '18 at 19:38

1 Answers1

1

If I put this in front of the variables, everything works fine.

In this scenario, you are adding properties to the queue object. Anything that has a reference to that object will also be able to get to its properties

However, if I remove the this keyword, I can't reach collection and index when I create a object.

In this case, you are creating local variables. They will only be in scope to code in the current function, as well as any nested functions.

Is there any way to reach these variables?

Only inside the constructor and any nested functions. By nested functions, i mean you could do this if you wanted:

    function MyQueue () {
      let localVariable = 3;
      this.logLocalVariable = function() {
        console.log(localVariable);
      }
    }

    const queue = new MyQueue();
    queue.logLocalVariable(); // logs 3;
    console.log(queue.localVariable) // logs undefined
Nicholas Tower
  • 72,740
  • 7
  • 86
  • 98