JavaScript Getter & Setters
For an upcoming article, I'm investigating the source code to Ghost - the platform this blog (and many others) run on.
I found this code in main/core/server/web/shared/index.js among other places in the code-base.
module.exports = {
get middlewares() {
return require('./middlewares');
}
};
I've got to be honest, I've not seen this in JavaScript yet; it's a getter method, obviously.
From MDN Web Docs:
The get
syntax binds an object property to a function that will be called when that property is looked up.
There are also setter methods and additional information can be found here: Defining getters and setters.
Ultimately, it allows you to directly define methods that are bound to this
. This is a very high-level and simplistic explanation of this syntax; please see the links for additional insight but as far as code-exploration goes, this should suffice.