Okay with the assumption that you are a ” something that reference newbie” and be aware I am probably two years in the game and just now understanding this behind javascript.
So as you may already know ‘Getters’ and ‘Setters’ are common archetypal methods in several other programming languages, specifically more within object-oriented languages like C#, Java, and Objective- C. Solet go ahead and dig right into it.
In most object-oriented languages, ‘Getters’ and ‘Setters’ refer to the properties of a given object. Getters are methods that return the value of that property, and as you may assume setters set them. Now Javascript has many object-oriented features, and this taps into some of those.
The reason they’re a thing and why they exist is a lot more clear in other languages in my personal opinion. So I will go on a tangent briefly so you can understand what I mean and the language of my choice will be Java. Okay, so let’s assume that you want to make some cars. To do that, in java we need a class called “Cars”
class Car {
private int year;
private string make;
private string model;
private string vin;
private string color;
}
To make a car, we’d do this
//create a new car that will be called infiniti
Car infiniti = new Car();
A result, since Infiniti is a car and we said that Cars have a year and a make, we know Infiniti has these.
Don’t worry I will each the code snippet stated above, take note of the word private? That means that only Infiniti knows its year and its make and most importantly, it’s own vin. What this means is that Java, the following would not run and would fail:
CarJacker someRandomThief = new CarJacker();
someRandomThief.attemptingToGetVin(infiniti.vin);
In JS, you can do that –access the property directly through the dot operator. Now, why on the earth would you want that in a language? Because it makes things easier to maintain, it makes code safer and of course cleaner in the long run. When those properties are private, you can prevent other parts of a program you’re writing from accidentally changing them and weird bugs from showing up. this protecting of properties from outside objects is called encapsulation and is hugely important in OOP. Some would consider this a great feature but it can also be super annoying, because how often do we have variables that we never want any other part of the program to access?
Finally entering into the fray are ‘Getters’ and ‘Setters’. Commonly in Java and other languages as stated before getters and setters methods which allow “outsiders” to access those properties.
class Car {
private int year;
private string make;
private string model;
private string vin;
private string color;
public string getVinNumber() {
//this method returns the vin number of the car
return vin;
}
public void setVinNumber(string newVinCreated) {
//'this' refers to the object itself --Infiniti, etc
this.vin = newVinCreated;
}
}
As a result, outside objects can now interact! So if our car thief got lucky, it could instead:
CarJacker someRandomThief = new CarJacker();
//now we just stole a new car
someRandomThief.attemptingToGetVin(infiniti.getVinNumber());
Now in Java, and the other languages that I know of, ‘getter’ and ‘setter’ are common but not formal: ie, I could call getVinNumber() something as unrelated as UnlessMethodName() and it would still work–but would just be a note helpful name.
I know this was much longer than some of my other posts but it all has a purpose of course. Now within the js you have “get” and “set” doing those things because of the OOP idea of encapsulation and getters and setters, which is a useful idea–and sort of a hard sell in particular in js, where it’s not enforced because you don’t have to declare whether something is private, so it becomes a sort of arbitrary weird layer unless you understand the original intended purpose.
Ultimately the pattern behind is a thing I dislike about JavaScript — and I’ll be as honest as I can be, I’ve read that documentation so many times before, and even again as I was preparing this post and without much it still makes very little sense to me, and I’ve had a lot more experience since the first time I read it along with a hunk of object-oriented under my belt… and it still is really hard to follow.
In short, get latest() is a means of having things happen when you want the value of latest, and enables that to happen–since you have to have a function or method for things to happen–while still letting you skip the “()” because it’s single value representing a property.
- JS has a prototypical inheritance, not class-based. Which is far from the only difference, but a huge one.
- As with everything, there are differences of opinion and you can still have crazy OOP code, it’s not perfect but that’s the intended purpose.
- I strongly feel that stuff like Raccoon raccoon = new Raccoon() leads too quickly to semantic satiation and can lead to more bugs and always go for more specific or least different variable names when I can, but anyways.