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
Allow decorating the "constructor" as a function #419
Comments
@Jamesernator I have three main concerns with moving in this direction.
Given these considerations, I think that the best path forward would be to create a follow-on proposal for this functionality. |
Performance wise I don't see that anything should make it worse than subclassing, particularly given that the function passed would essentially be almost the same thing as
I agree
I think it's fine to delegate things like this to future proposals, although I think it is important to consider how these sort've things would work, especially in regards to integration with the current proposal. What I had above is fairly vague about integration with various parts of the decorator proposal, particularly such as how it would interact with something like the metadata API. By considering these sort've things now it can be more easily considered as to how it would combine into the existing proposal or if there are things that need to be changed about the current proposal for this and/or other future proposals. Having said that I don't think there's anything particular in the current design that would block this sort've feature being added in future, but it's possible I've missed something about how any of the features work. |
Absolutely agree there, and we've been very conscious about leaving space for possible extensions, both syntactically and functionally. If you see any issues with the current proposal that could prevent |
Closing this as a duplicate of #48 |
This is an issue to reconsider allowing decorating the
constructor
function directly (as per this request).Being able to decorate the
constructor
function itself directly would make decorators that would usually operate on methods behave more similarly with the constructor rather than having two kinds of certain decorators.In particular the sort've decorators of interest are ones such as param/return checking, wrapping return values (e.g. wrapping
this
with proxy), memoization and so on.For methods when a wrapping decorator is applied, in general external code could not actually know of the decorator, however when creating decorators such as the above with subclass-returning-decorators each such decorator adds an extra class into the chain, in general these classes may not be desirable to be accessible. As an example for a decorator like
@validateParams(isNumber, isString)
under the current spec could only return a subclass, but consumers of the class could trivially access the class where this decorator is not applied by looking up the prototype e.g.:This is quite different to applying the decorator to any other method, or function, in general this sort've thing cannot be broken from the outside unless the decorator explictly opts in to do so.
My suggestion would be to adopt the ability to decorate the internal
constructor
method-esque thing directly similarly to as suggested previously.Essentially decorating
constructor
would behave primarily like a method decorator but being passed a function which constructs the instance passed as a value. The function given to this decorator would be effectively like thesuper()
"function" in a subclass except without setting the decorated function'sthis
.i.e. Usage would be like:
The text was updated successfully, but these errors were encountered: