Decorators for functions
Frankie Bagnardi
f.bagnardi at gmail.com
Tue Oct 20 14:40:17 UTC 2015
Decorators can be both used to wrap things and to annotate them. For
example, here we're setting a flag with the `web.method` function which is
used by by this fictional 'web' framework. The others are used as
middleware that modify the function arguments at each step.
```js
export default
@web.method('post')
@web.parsesJSON()
@web.expectsBody({
emailAddress: String, password: String,
})
function handleLogIn(req, res, body){
// ...
}
```
In this example we have a React component that's just a simple function. We
want to wrap it with a high order component.
```js
@providesSomething()
function MyComponent({something}){
// ...
}
```
Here we're using dependency injection:
```js
@di.provide(Calculator)
function add(calc, a, b){
return calc.add(a, b);
}
add(1, 2) === 3
```
I'm not completely sold on if these are good ideas. It might be more
confusing than it's worth.
On Tue, Oct 20, 2015 at 7:23 AM, Eli Perelman <eli at eliperelman.com> wrote:
> More drive-bys.
>
> I could see decorators as a nice way to define "functional" behavior for
> generic functions:
>
> @curried
> var add = (a, b) => a + b;
>
> @memoize
> var fetch = (url) => /* logic */;
>
> Eli Perelman
>
> On Tue, Oct 20, 2015 at 8:42 AM, Kevin Smith <zenparsing at gmail.com> wrote:
>
>> I would just say that it is odd in the extreme that a group of
>>> world-leading language designers would just throw in the towel when
>>> confronted with a pretty small roadbump, instead of figuring out ways to
>>> solve it.
>>>
>>
>> Another drive-by...
>>
>> The trick is introducing new features without exploding the number of
>> rules which must be remembered in order to use that feature.
>>
>>
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20151020/7b53249b/attachment.html>
More information about the es-discuss
mailing list