I have some classes which consist of many short getters/methods.
Example:
get jQuery() {
return this.pageConfig.jQuery || jQuery;
}
An arrow function with similar content may be written as follows:
() => this.pageConfig.jQuery || jQuery;
Which is a one-liner and thus consumes only 1/3 of vertical space.
But it is not a getter nor a method.
Is there a recommended way of writing getters/methods in the form of a one-liner? (if possible without curly braces and without the return
keyword)
My getters and methods need not modify the object. They are just reading.
For a one liner, just go with this:
get jQuery() {return this.pageConfig.jQuery || jQuery;}
Arrow functions are not for method definitions because they use the lexical this
, whereas a method wants the object this
. Remember that while arrow functions are a nice shorthand, they also affect the value of this
in a way that is not usually compatible with method definitions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With