-1

Below we define a private variable boy for a would-be instance of the class 'Forrest'. By doing this JavaScript signals of no mistakes. But neither the instance, nor Forrest.prototype or Forrest function object itself show no signs of hosting this variable.

class Forrest {
  constructor() {
    let boy = "Bobby";
    girl: "Marry";
  }
}

const f = new Forrest();

However, we can easily get access to this private boy variable via vanilla JS constructor function.

function Forrest() { 
  let boy = "Bobby"; 
  this.getBoy = function() { 
    console.log(boy);
  } 
} 

const f = new Forrest();
f.getBoy(); // Bobby 

How do we get access to this private (local) variable in ES6 class?

Maksym Dudyk
  • 1,082
  • 14
  • 16
  • 4
    `girl: "Marry" ` is not a property. It's a [*labeled statement*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label) and does nothing. To create a property you would have to do `this.girl = "Marry"`. – Felix Kling Feb 19 '21 at 13:06
  • And `boy` is just a local variable with no persistence beyond the scope of the `constructor` function. – deceze Feb 19 '21 at 13:10
  • …and `let boy` creates a *local* variable that is not stored on the object but is only visible inside the constructor. – Bergi Feb 19 '21 at 13:10
  • (Btw, this is not different from ES5) – Bergi Feb 19 '21 at 13:10
  • Your "vanilla constructor" example has no resemblance to the class example. `f` is not an instance of `Forrest`, it's an anonymous function object. – deceze Feb 19 '21 at 13:44
  • Hint: there's no such thing as "private variables" in Javascript. Private fields are a thing though, but they look like this: https://stackoverflow.com/a/52237988/476 – deceze Feb 19 '21 at 14:00
  • Interesting. Thank you all for your comments. Yet, it seems that I can get access to a private variable via vanilla Function constructor, unlike in ES6 class. Here is an example: function Forrest() { let boy = "Bobby"; (function() { console.log(boy); })() } var f = new Forrest(); f instanceof Forrest // true – Maksym Dudyk Feb 19 '21 at 14:06
  • What does *that* demonstrate?! The `inner` function is just an IIFE which you might as well remove entirely. Then it's just down to defining a local function variable and logging it. – deceze Feb 19 '21 at 14:08
  • Would you please tell me how to get the same access to a 'closed' variable in ES6 class? – Maksym Dudyk Feb 19 '21 at 14:15
  • You don't. You're not even *really* getting any access to it in your "vanilla" example. You can't get both an instance of `Forrest` *and* the value of `boy` when you declare `boy` as a local function variable. `boy` needs to be a property of the instance of `Forrest`. As the answer below shows, that's done with `this.boy = 'Bobby'`, and that's in fact the same for ES6 and vanilla. – deceze Feb 19 '21 at 14:19
  • deceze, but I CAN "get both an instance of Forrest and the value of boy" by this: function Forrest() { let boy = "Bobby"; this.getBoy = function() { console.log(boy); } } var f = new Forrest(); f.getBoy() // Bobby Can I do the same in ES6 class? – Maksym Dudyk Feb 19 '21 at 14:26
  • Well, now you've added a property to the object that lets you access the value. Not much different than `this.boy = 'Booby'`. You're just using a closure instead of the bare value. Try *assigning* a new value to `boy` with just this; you can't. You only have indirect access to `boy`. You can do the same in an ES6 class with `this.getBoy = ...` in `constructor`. – deceze Feb 19 '21 at 14:31
  • Thank you, deceze. "this.getBoy = function() { console.log(boy) }" inside a contructor() {} will do the job of accessing a private variable in ES6 class. – Maksym Dudyk Feb 19 '21 at 14:53

2 Answers2

1

let boy is a variable only valid within the constructor's scope

girl: "Marry" is not a property at all

This is how you initialize and access attributes

class Forrest {
  constructor() {
    this.boy = "Bobby"
    this.girl = "Marry"
  }
}

const f = new Forrest();
console.log(f.boy)
console.log(f.girl)
jo3rn
  • 1,291
  • 1
  • 11
  • 28
  • We can get this 'boy' variable via vanilla constructor function. Why we cannot get it via ES6 class? function Forrest() { let boy = "Bobby"; return function() { console.log(boy); } } var f = new Forrest(); f(); // Bobby – Maksym Dudyk Feb 19 '21 at 13:31
  • @Maksym Because that "constructor function" doesn't return an *object instance* like the class does, it returns a *closure*. Very different examples. – deceze Feb 19 '21 at 13:36
0

// The answer turns out to be:

class Forrest {
  constructor() {
    let boy = "Bobby"
    this.getBoy = function() { // This is it!
      console.log(boy)
    }
  }

}

const f = new Forrest()
f.getBoy() // Bobby
Maksym Dudyk
  • 1,082
  • 14
  • 16