0

Is this a right way to set a variables as private?

function test(a, b){
        let aa = a
        let bb = b
        return {
            getA: () => {
                return aa
            },
            getB: () => {
                return bb
            }
        }
    }

Now I can't access aa and bb directrly. And if I put this keyword infront of the variables, will it be accepted as an object? I know that in JavaScript functions are objects but it become more like object syntax. I am confused a lot from all the JavaScript options. It seems like there is no conventions in anything. The this type:

function test(a, b){
        this.aa = a
        this.bb = b
        return {
            getA: () => {
                return this.aa
            },
            getB: () => {
                return this.bb
            }
        }
    }

Both examples seems equal to me. The summed question - Is the privace of those variables is achieved in those ways ?

Toma Tomov
  • 1,241
  • 10
  • 35
  • 2
    Have a read up on JS deign patterns, it seems your trying to create a [revealing module pattern](https://addyosmani.com/resources/essentialjsdesignpatterns/book/#revealingmodulepatternjavascript), but there are other options. – Liam Nov 30 '18 at 11:28
  • Will take an I on it for sure. In fact, I am trying to understand which are the right ways at all in JS. I know for different situations different solution can be used. Just trying to sum up the things a little. – Toma Tomov Nov 30 '18 at 11:29
  • 2
    Be careful of "right way". Typically there is no right way, just different ways. – Liam Nov 30 '18 at 11:30
  • @Liam Yes yes. Just sayed like this. Understand that :) It is like there is so many ways that I can do the things here. – Toma Tomov Nov 30 '18 at 11:31
  • Is it problem? You mean it has to be like that `this.getA` right ? Or it is like a convention ? Like the constructor functions have to start with capital letter lets say. – Toma Tomov Nov 30 '18 at 11:35
  • 2
    The second block is not private. `aa` is scoped to the `window` object – Liam Nov 30 '18 at 11:35
  • @CertainPerformance the second one is not private. And if the function is called without `new` the variables/properties are not just public but global. – Thomas Nov 30 '18 at 11:43

1 Answers1

1

These blocks of code are not equal. See below:

function test(a, b){
        let aa = a
        let bb = b
        return {
            getA: () => {
                return aa
            },
            getB: () => {
                return bb
            }
        }
    }
    
let testResult = test(1, 2);
console.log(testResult.aa)
//error
console.log(aa);

vs

function test(a, b){
        //this here === window
        this.aa = a
        this.bb = b
        return {
            getA: () => {
                return this.aa
            },
            getB: () => {
                return this.bb
            }
        }
    }
    
let testResult = test(1,2);
console.log(testResult.aa);
//can get aa
console.log(aa);

this in your second block of code is the window object. So aa is not private. Where as in the first block, it's block scoped and not accessible. So aa is only "private" in the first one.

Is this the "right" way?

As I mentioned this is just a design pattern (revealing module pattern). Be wary of "right" way. "right" is subjective and depends on context. There is no "right" way, but this is a common way of structuring this kind of thing.

This is just using a closure to scope a variable. There is no "private" declaration, as such, in js. Just scoped variables.

Liam
  • 22,818
  • 25
  • 93
  • 157
  • Oh, I think I get it now. If declare variabl `let myVar = new test(1,2)` than this will point to the `myVar`. So from what I understand `this` shouldn't be used in functions ( which are not made to be constructors ). Am I on the right way ? :) – Toma Tomov Nov 30 '18 at 11:39
  • @Tnc Andrei No no. Just testing. Wont use anything that I do not understand :) – Toma Tomov Nov 30 '18 at 11:40
  • 1
    `this` can be used @TomaTomov , but you need to be aware as to what [`this` actually means in the scope that your using it](https://stackoverflow.com/questions/4195970/what-does-this-mean). – Liam Nov 30 '18 at 11:41
  • Get it! Thank you very much! – Toma Tomov Nov 30 '18 at 11:43
  • 2
    The second example should be called like this: `let testResult = new test(1,2);`. Calling test with the new keyword will return an object and everything attached to `this` will belong to test instead of window – Tnc Andrei Nov 30 '18 at 11:43