While playing with decorators on TypeScript playground I've noticed, that on the line #3 transpiled code checks the existence the Reflect.decorate
function.
What is this function about? I didn't manage to find this neither on SO, nor on MDN documentation about Reflect
.
A Decorator is a special kind of declaration that can be attached to a class declaration, method, accessor, property, or parameter. Decorators use the form @expression , where expression must evaluate to a function that will be called at runtime with information about the decorated declaration.
Decorators are the way of wrapping one piece of code with another or apply a wrapper around a function in JavaScript. Decorators are the design pattern that allows behavior to be added to an individual object, either statically or dynamically without affecting the behavior of other objects from the same class.
target: Constructor function of the class if we used decorator on the static member, or prototype of the class if we used decorator on instance member. In our case it is firstMessage which is an instance member, so the target will refer to the prototype of the Greeter class. propertyKey: It is the name of the property.
Parameter Decorators A parameter decorator is defined just before a parameter declaration. It is applied to the function for a class constructor or method declaration.
The answer to your question lies right in the next line in the transpiled code:
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
To understand it you need to keep in mind these things:
Now, if you read again, you see that "Reflect.decorate" is a method that goes through the list of decorators that you intend to apply, then identifies the type of decorator based on the number of arguments, and finally calls the decorator with the right arguments (based on the type that it detected).
Reflect.decorate
if they have it right there?Because at some point in the future they expect Reflect.decorate
to become part of the ES standard and then be implemented in browsers accordingly. At that moment, they'd rather use the native method than this polyfill.
As this method is part of "ES.later" (not even ES.next), no browser has implemented it natively yet, and for that reason nobody documents it yet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With