ITADN

Decorated classes do not have to be constructors?

#562Openmarrus-sh 创建于 2025-08-14
M
marrus-shcommented
Classes are constructors: they can be called with `new` and can be used as the `new.target` when calling other constructors. I expected decorated classes to share this trait, but it does not seem to be the case: **15.7.7 ApplyDecoratorsToClassDefinition** only checks `IsCallable` on the returned class, not `IsConstructor`. Hence, while this code always succeeds: ```js class Foo { /* anything */ } Reflect.construct( function() {}, [], Foo, // newTarget must be a constructor ); ``` this code is no longer guaranteed to: ```js @someDecorator class Foo { /* anything */ } Reflect.construct( function() {}, [], Foo, // newTarget must be a constructor ); ``` And it seems indeed possible to decorate a class and return an arrow function, which can never be called with `new`, inverting the calling behaviour of the original class. Or am I missing something? What is the rationale for this behaviour?
2 条评论