javascript - Single line shortform for non-arrow function? -
sometimes, in classes, need single line function.
for instance:
class blah { getvisibilityanimvalue() { return this.state.isvisible ? 100 : 0 } }
in es6 there friendly way? tried omit surrounding curlies , omitting return it's not working.
in es6 there friendly way?
no, there not. think know, in order use this
refer calling instance, cannot use arrow function (which uses lexical this
rather this
based on how called). so, have use conventional es6 method definition. such, need braces {}
, need return
.
the es6 class syntax @ least saves es5 scheme of:
blah.prototype.getvisibilityanimvalue = function() {...}
i tried omit surrounding curlies , omitting return it's not working.
yep, still required.
if had lot of methods worked , used identical logic, factor logic shared function.
// define once , use many places function zval(val) { return val ? 0 : 100; } getvisibilityanimvalue() { return zval(this.state.isvisible) }
but, simple, it's arguable whether "better" since involves function call , makes little harder see (in glance) does.
Comments
Post a Comment