DEV Community

Discussion on: Why decorators should be available everywhere

Collapse
 
seanmclem profile image
Seanmclem

How is the code below a decorator connected to the decorator above it? Implicitly?

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀 • Edited

Great question, yes decorators are implicitly connected to the next valid block of code they can find (JavaScript doesn't care about whitespace), that's why you can place a decorator above a method or inline.

@myDec someMethod(){}

Or

@myDec(someInterceptingParam) someMethod(){}

Or

@myDec
someMethod(){}

Or in my case

@get ["/home"] // <- that's a method name
(){}

I could have done (probably should have done)

@get('/home') handleHomePage() {}

But you can see that this allows a URL to be used by two methods as the URL is now not tied to the method name and also there is more to type.