Skip to content

Class constructors cannot be invoked without 'new' #1123

Closed
@p120ph37

Description

@p120ph37

Currently, ObjectWrap<T>::ConstructorCallbackWrapper has a guard against calling the constructor as a javascript function without the "new" keyword (TypeError: Class constructors cannot be invoked without 'new'). In a pure Javascript project, or in a non node-addon-api native project, it is possible to trap this sort of scenario and do something special other than abort -- often simply re-call the function as a constructor and return the new object, but potentially other clever things too.

If we could change the ObjectWrap<T>::ConstructorCallbackWrapper implementation to delegate to an overridable public static method when !isConstructCall rather than always aborting (e.g. Value T::NonConstructFunction(const Napi::CallbackInfo& info) with a default implementation that throws the usual TypeError), that would allow for more flexibility in porting older modules to use node-addon-api without changing their existing semantics.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions