-
Notifications
You must be signed in to change notification settings - Fork 127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"this" inside the decorator #72
Comments
@mjesun Actually, the this is not defined. The decorator is considered to be a free function and I believe this to be the correct approach. That way, you can define more complex decorators where you can provide your own |
Maybe I'm missing something; but I guess you can't provide your own this, unless you bind your method beforehand; since you do not have any control on how the method is invoked. It's also simpler for me to expect that a decorator defined as |
I'm on the opposite side, the declaration and invocation does not happen at the same time, when you write code like: @a.b is just a declaration which just like: var hereIsADecorator = a.b; So when decorator is invoked, it is: hereIsADecorator(); which should not provide any |
I can get that point; however I don't see the benefit of it. If I assume you want to keep it Still, the proposal is yours, so if I can't convince you I think we can close the ticket 😃 |
I'm not the author but I believe assuming a For example if we implement a decorator like this: // decorators.js
export default {
name: 'whatever',
foo(target, key, descriptor) {
target._hasDecorator = this.name;
},
bar() {
// ...
},
// ...
}; but is used like this: import decorators from 'decorators';
let {foo, bar} = decorators;
export default class OhMyGod {
@foo
@bar
work() {}
} The If const context = {name: 'whatever'};
function foo() {
}
export default {
foo: foo.bind(context)
}; This is much safer |
What should be the value of "this" inside the decorator? For example if you do something like:
When being inside
decorate
, the value ofthis
should beDecorators
, right?I'm asking that because I haven't been able to find this inside the proposed specification, and I think it's nice to have it defined. Also, if that's the case, the current plugin for babel (the
babel-plugin-transform-decorators-legacy
) should be changed to support it. Right now the compilation is done as:Which would be incorrect (
this
isundefined
in strict mode, or the global object in loose mode).The text was updated successfully, but these errors were encountered: