Impressive. Javascript is truly versatile as fuck...
Yup. It can bite you in the ass for sure, but for all the terrible things about JS it can be really fun sometimes.
As to why I don't just access them directly obj->name or obj.name, I was told that is bad practice as it ends up fucking you on larger projects, not sure how but getters/setters must be a thing for a reason.
That is... an opinion that some people hold and for which seemingly convincing arguments can me made. There is a long running clash of ideologies in programming and the whole getters/setters thing is one paradigmatic place where that gets played out. There's the notion of "information hiding" that's really near and dear to people in the OOP world. The classic example that's supposed to illustrate this point involves rectangles (other shapes will do, but area is easy for rectangles):
function Rectangle(width, height) {
this.width = width
this.height = height
}
Rectangle.prototype = {
getWidth: function() { return this.width },
getHeight: function() { return this.height },
getArea: function() { return this.width * this.height }
}
function CachedRectangle(width, height) {
this.width = width
this.height = height
this.area = width * height
}
CachedRectangle.prototype = {
getWidth: function() { return this.width },
getHeight: function() { return this.height },
getArea: function() { return this.area }
}
The notion here is that an instance of Rectangle can be substituted for CachedRectangle with no changes to any code so long as the getters are the only thing that's being used. This is a hedge which is supposed to save you programmer time if you decide that it's better to precompute area or whatever. And the example works, if you're using properties directly you really couldn't substitute one for the other without issue (well actually with ES6 you could, but being charitable (and practical, for the immediate future) that's irrelevant).
I'm not really a fan of the philosophy that informs people making this argument. I think this is a pretty example but in my experience at least (and it seems like others have the same experience) there is a tremendously greater amount of effort spent on other things like managing mutable state or concurrency issues than there is on swapping out implementations of equivalent abstract data types. In fact I'd argue more time is actually spent in writing these dumb getter/setter methods than is saved by them.
I'm pretty opinionated on the subject though, and it's worthing noting that the OOP approach (in favor of getters/setters on everything) is more popular today (in terms of numbers) than competing views. I think the important takeaway is that you should assess the situation for yourself, there is a strong enough argument for either position that which ever way you think is best, so long as it's based on evidence, is justifiable.
Nerds
u jelly JELLYFISH.JPG