jayphelps/core-decorators

[suggestion] @chainable decorator for functions

Closed this issue · 4 comments

Chaning is a common pattern I was wondering what stops from a method for creating chainables ?

Example

class Foo{
    @chainable
    bar(){
    }

    baz(){
    }
}

const f = new Foo();
f.bar().bar().bar().baz();

IMO just using return this; is more idiomatic. Thoughts on why a decorator would be an improvement? Wouldn't it be confusing because someone could write return somethingElse; but all return values would effectively be silently thrown away?

class Foo {
  bar() {
    return this; 
  }

  baz() {
  }
}

const f = new Foo();
f.bar().bar().bar().baz();

I agree with jayphelps. What does improve this decorator?

@jayphelps maybe I just liked using a decorator a little more. No other helpfullness.

Personally @chainable would simply make it obvious that a function is chainable, Also when using @chainable you explicitely expect no return

@darkyen Very much appreciate the input. As of now I think this is better left to return this. Thanks again!