Skip to content
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

another guard against omitting the new keyword #342

Open
lewisje opened this issue May 29, 2016 · 0 comments
Open

another guard against omitting the new keyword #342

lewisje opened this issue May 29, 2016 · 0 comments

Comments

@lewisje
Copy link

lewisje commented May 29, 2016

This is well-known but may also be considered an antipattern, especially because it cannot be encapsulated, and instead the constructor name must be updated in three places, and the argument list in two, if either is changed, all because Function#apply cannot perform a constructor call:

function Constructor(arg, arg2) {
    if (!(this instanceof Constructor)) {
        return new Constructor(arg, arg2);
    }
    // rest of constructor code
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant