Decorators on functions

后端 未结 3 1339
有刺的猬
有刺的猬 2021-01-31 14:31

I see that babel.js decorators (available in \"stage 1\") implement the spec at https://github.com/wycats/javascript-decorators. It appears that decorators are limited to (1) cl

3条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-31 14:48

    To execute a decorator, you evaluate an expression and doing that prevents hoisting (even for a variable declaration, the right-hand side of an assignment stays put). Therefore, it is not compatible with function declarations being hoisted.

    As a work-around, I suggested that function expressions, generator function expressions and arrow functions could be enabled to be decorated:

    const func = @someDecorator('abc') (x, y) => { return x + y };
    

    Alas, that wasn’t met with much enthusiasm: Decorators for functions

提交回复
热议问题