tc39/proposal-decorators

If a non-constructable value is returned, then an error is thrown?

trusktr opened this issue · 5 comments

Does this mean decorators can still return plain functions? f.e.

function log(Class) {
  return function () {
    console.log('instantiating class')
    return new Class()
  }
}

@log
class Foo {}

new Foo() // logs "instantiating class"

?

Or does it have to be a class?

Class decorators must return a callable value, it does not need to be constructable, per the spec: https://arai-a.github.io/ecma262-compare/?pr=2417&id=sec-applydecoratorstoclassdefinition

Oh ok. So then any function is ok to return, not just anything callable with new? If so, the README could use an update.

Yes, and proxies are also ok to return, as they can intercept calls.

Closing as this was addressed by #483