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?