[ES7] Descorator:evaluated & Call Order

Source: Internet
Author: User

When multiple decorators "apply to a" declaration, their evaluation is similar-function composition in mathematics . In this model, when composing functions f and G, the resulting composite (F ° g) (x ) is equivalent to f(g(x)).

As such, the following steps is performed when evaluating multiple decorators on a single declaration in TypeScript:

    1. The expressions for each decorator is evaluated Top-to-bottom.
    2. The results is then called as functions from Bottom-to-top.

If we were to use decorator factories, we can observe this evaluation order with the following example:

function f () {Console.log ("f (): evaluated"); returnfunction (target, PropertyKey:string, Descriptor:propertydescriptor) {Console.log ("f (): called"); }}function g () {Console.log ("g (): Evaluated"); returnfunction (target, PropertyKey:string, Descriptor:propertydescriptor) {Console.log ("g (): Called"); }}classC {@f () @g () method () {}}/*f (): Evaluatedg (): Evaluatedg (): Calledf (): Called*/

[ES7] Descorator:evaluated & Call Order

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.