(ES6) ES6 (2016)

Decorator (DC01)

   1:  @decoratorFunction
   2:  class MyClass { /* ... */ }
   3:  
   4:  @decoratorFunction
   5:  function myMethod() { /* ... */ }
   6:  
   7:  @decoratorFunction
   8:  get myProperty() { /* ... */ }



Decorators are a form of metaprogramming in JavaScript. They provide a way to wrap additional functionality around existing code (classes, methods, accessors, properties, or parameters) without directly modifying the original code itself. Think of them as "decorators" that enhance or modify the behavior of the underlying code element. In essence, a decorator is a higher-order function that takes a target (the thing being decorated) and returns a modified version of that target or something that replaces it.

Decorators use the @ symbol followed by the decorator function's name, placed directly before the code element being decorated:


Decorator Types:


How Decorators Work, A decorator function takes the following arguments:


The decorator function can then modify the target, descriptor, or return a new descriptor. This allows it to wrap additional logic around the original code.

Key Points to Remember:






Decorator context:






ES6 context:



Comments ( )
Link to this page: http://www.vb-net.com/JavascriptES6/DC01.htm
< THANKS ME>