Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 getter/method without curly braces

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.

like image 678
ideaboxer Avatar asked Mar 01 '17 19:03

ideaboxer


1 Answers

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.

like image 160
jfriend00 Avatar answered Oct 01 '22 02:10

jfriend00