What is the decorator
Decorator is actually an ordinary function to modify classes and class methods.
such as:
@test
class DecoratorTest {
}
function test(target) {
target.testable = true;
}
The target argument is that it modified the class
which represents the class to DecoratorTest add a static property testable, is equivalent to:
class DecoratorTest {
public static testable = true;
}
If you feel that a parameter is not enough, you can then set a layer of function parameters used to pass out
like this:
@testParam(true)
class DecoratorTest {
}
function testParam(boolean bool) {
return function test(target) {
target.testable = bool;
}
}
This makes it a more flexible.
We're just a decorator class to add a static property, plus empathy instance properties on the prototype class need only to add a property it on the line
@testParam(true)
class DecoratorTest {
}
function testParam(boolean bool) {
return function test(target) {
target.prototype.testable = bool;
}
}
::: warning
decorator class behavior changes occur in your code compile phase, rather than the operational phase
:::
Which can be used in
Modification can not only decorate a class, the class can also be modified in the properties and methods of
what modifications you on what is in front,
When the modified class attributes and methods, decorators function accepts three parameters
function readonly(target, name, descriptor) {
descriptor.writable = false;
return descriptor;
}
target is the target object, name is a modified attribute name, attribute descriptor that is described in the object
descriptor ships so long
{
value : specifiedFunction,
enumerable: false,
configurable: true,
writable: true
}
It defines whether the attribute can be enumerated, is readable, whether disposed
above readonly attribute decorator modified unwritable
similar
Object.defineProperty(target, name, {
value : specifiedFunction,
enumerable: false,
configurable: true,
writable: false
})
Which can not be used
Js modifications in function can not be used to modify, as a function js lifting function in the presence of pre-compilation stage
Third-party libraries
core-decorators.js
This module encapsulates several commonly used decorator
- @autobind the method of this binding the original object
- @readonly the properties and methods can not write
- @override check subclass method correctly covers the parent class method of the same name, will get an error if incorrect
- @deprecated a warning will be displayed on the console, indicating that the method would repeal