-
Notifications
You must be signed in to change notification settings - Fork 107
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
@bound example - documentation suggestion #55
Comments
I'm not sure that the performance property you're referring to is predictably the case across implementations. I'd rather not use performance as the design principle here. Let's continue discussion about more appropriate examples on the other issue that you linked to above. |
A bigger downside than performance of this approach in the React community is that it makes it very hard to mock out these functions in tests. Because it's common, people invariably run into this problem, file bugs on enzyme, and are corrected and told that arrow functions should never go in class properties, stop using them, and then don't run into problems after that. |
Actually I wasn't referring to performance, but rather issues relating to the method not being on the prototype at all anymore when using a property initializer and arrow function. As I said in the other issue, I don't think the Medium article's claims about performance are even correct. I'm happy to continue the discussion in the other thread, but just to make sure -- you saw that that issue is in the class fields proposal, right? Wouldn't this repo be the more relevant place for a discussion about decorator examples? |
Yes, I agree that this repo is better for such discussion. Sorry for my misunderstanding. |
Reposting my suggestion from the other thread (to make the discussion easier to follow):
There are certainly realistic examples of a
|
I think of " " |
Sure, that would work. For the sake of an example I'm not sure all those separate decorators would be needed, just as they might not be needed in every project if you're not going to be modifying just |
I just realized that a
The problem here is that unless the placement of instance properties has completely changed since the first version of this proposal (and no such change was mentioned in the list of changes for member descriptors), then the
Using Babel with the older decorators proposal (babel-plugin-transform-decorators-legacy), I had to write a rather ugly workaround to get a I suppose this could be solved by additionally having a class-level decorator that would be required in order to use the property-level Perhaps I should open a new issue about the new concerns I raised above...or am I misunderstanding something? *In this particular example of a domain model, it would be even nicer to have a generic way of setting initial state as I described here, but that's not relevant to the decorator implementation, probably. |
@mbrowne Effectively, in my framework, I had to apply them ( I was thinking of storing decorators in the property descriptor, then having something that queries them and apply things like "finalizers", ... They could also be queried by user code. |
What if the |
@mbrowne I'm not sure to understand... Assuming : function readonly(/* All the proper arguments */)) {
// Do the magic here
} What I mean is : class MyClass {
@readonly id
} Might be equivalent to : const MyClass = function() {};
Object.defineProperty(MyClass.prototype, 'id', {decorators: [readonly()]}); |
@doodadjs Have you read this document? https://github.com/tc39/proposal-decorators/blob/master/METAPROGRAMMING.md. Your above code doesn't seem to reflect the current proposal, at least as I understand it. |
@littledan @ljharb I looked in the spec for where the |
@mbrowne I've edited my comment. Is it better ? |
@mbrowne The problem I see with that approach (a function instead of a class) is on how to identify applied decorators... But that's another issue. |
@doodadjs I'm still confused by your example; are you trying to show how you would implement it using the current proposal, or proposing some alternative to the current proposal? A decorator in the current proposal would be equivalent to something like the following, assuming the decorator didn't change the default 'instance' placement:
...obviously this is not intended to show what real transpiled code would look like, but just the concept (also it doesn't handle the case of multiple decorators on the |
@mbrowne Sorry for the confusion.... I suggest to store the decorators within the descriptor, and make them query-able. |
See #58 for further discussion about how the |
Resolved via #59 |
It has become a common practice (especially in the React community) to create bound methods using a property initialized to an arrow function:
So at first I was surprised to see that the readme for this proposal has a
@bound
decorator as one of the examples, given that using an arrow function seems more straightforward. Then I found this:tc39/proposal-class-fields#80
...and I remembered that I myself had previously encountered issues with inheritance when using arrow functions in this way.
I'm guessing that this is why a
@bound
decorator is used as an example - since it's a more consistently readable way to bind methods, and thus preferable to an arrow function.In any case, it could be helpful to mention something about this in the readme, in case others also wonder, "Why not just use an arrow function"? I understand if this is a low-priority change, but thought it was worth suggesting.
The text was updated successfully, but these errors were encountered: