0

I am refreshing my memory with OOP in JavaScript and I get a bit confused. I just pick one of my project and try to convert it to OOP. Is 'get' key word really important? Let see the code below:

class Cipher {
    constructor (str) {
        this.str = str;
    }

    normalizedPlainText() {
        return this.str.replace(/\W/g,'').toLowerCase();
    }

    size() {
        return this.normalizedPlainText(this.str).length;
    }

    isValid() {
        if ( this.size(this.str) >= 50 ) return true;
    }

    squareRoot() {
        return parseInt(Math.sqrt(this.size((this.str))));
    }

    nbrRows() {
        return this.squareRoot(this.str);
    }

    get nbrCols() {
        if ( Math.pow(this.squareRoot(this.str), 2) === this.size(this.str)) return this.squareRoot(this.str);
        else return this.squareRoot(this.str) + 1;
    }
}



    const cipher = new Cipher('Your description gives people the information they need to help you answer your question##8.');
    console.log('sqrt '+cipher.squareRoot())
    console.log('Nbr rows ' + cipher.nbrRows()) //good output
    console.log('Nbr cols ' + cipher.nbrCols) // good output too

When designing my program I was wondering if I could use 'get' or not. So O did try as you can see on get nbrCols(). If I put get nbrRows() and call it cipher.nbrRows() I get an error unless I change the way I call it as this cipher.nbrRows. So the conclusion I have is this: it's up to you. If you use 'get' call it without () or I you don't use it call it with (). Am I wrong or do I miss something?

Abel LIFAEFI MBULA
  • 95
  • 1
  • 1
  • 10
  • 1
    You are correct, you can use `get col()` and then call the function as if it’s a variable. You can also just create a function `getCol()`. That way it’s more clear that a function is being used. It’s up to you what you prefer but I think functions are more readable. – Kokodoko Jun 25 '19 at 11:11
  • https://stackoverflow.com/questions/812961/getters-setters-for-dummies – Teemu Jun 25 '19 at 11:33

2 Answers2

1

By using the get syntax, you're adding a getter to the object, these were introduced in ES5 (2009).

See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get

You could of course add an explicit getProperty function instead of using the getter.

You don't use parenthesis when accessing a getter,

Whichever technique you use is up to you, each has its own advantages,

For example:

let person = { 
   _age: 42,
   get age() { 
       console.log("getter called!");
       return this._age;
   },
   set age(age) {
       console.log("setter called!");
       this._age = age; 
   },
   getAge() {
       return this._age; 
   }
}

console.log("Age: ", person.age);
console.log("Age (using getAge()): ", person.getAge());

// Assign age using setter 
person.age = 21;
console.log("Age (after set): ", person.age);

   
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40
1

It's up to you. If you use get call it without () or if you don't use get call it with ().

Yes, exactly. You should use getter if want the value to be accessed like it was a data property, and only when the computation doesn't mutate your instance.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375