Home >Web Front-end >Front-end Q&A >What is the use of es6 decorators?
In ES6, decorators are used to annotate or modify classes and class methods. It is a class-related syntax; the decorator is a function that is executed at compile time and adds some to the class or attribute methods under the class. Control conditions are usually placed in front of the definition of classes and class methods. They can be divided into two types: class decorators and class method decorators. The syntax is "@function name".
The operating environment of this tutorial: Windows 10 system, ECMAScript version 6.0, Dell G3 computer.
Decorator: It is a class-related syntax that is used to annotate and modify classes and class-related methods and properties. Many object-oriented languages have this feature. Generally related to class, don't use ordinary methods.
The decorator is a kind of function. It is written as @function name. It can be placed before the class and class method definitions. The decorator is to execute the function and add some control conditions to the class or the attribute method under the class
Decorator
Drive some other code to the class or class attribute, so that the code can be reused
Decorators are mainly used for: decoration classes, decoration methods or attributes
Examples are as follows:
Decoration class
@annotation class MyClass { } function annotation(target) { target.annotated = true; }
Decoration methods or attributes
class MyClass { @readonly method() { } } function readonly(target, name, descriptor) { descriptor.writable = false; return descriptor; }
[Related recommendations: javascript video tutorial, web front-end]
The above is the detailed content of What is the use of es6 decorators?. For more information, please follow other related articles on the PHP Chinese website!