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

Can class.initialize be called more than once? #1

Open
ljharb opened this issue Jan 6, 2019 · 7 comments
Open

Can class.initialize be called more than once? #1

ljharb opened this issue Jan 6, 2019 · 7 comments

Comments

@ljharb
Copy link

ljharb commented Jan 6, 2019

No description provided.

@littledan
Copy link
Owner

Hmm, I wasn't thinking of making limitations on this. Do you think there should be? What sorts of guarantees would that lead to? (A constructor can recurse anyway...)

@ljharb
Copy link
Author

ljharb commented Jan 7, 2019

Can you elaborate on a constructor recursing? Circular prototype chains are forbidden and the super return trick would only work on one object, no?

@littledan
Copy link
Owner

I don't mean any of that, it's just that if we made such a restriction, it would be possible for the constructor to make further calls to new, and use that to class.instantiate many objects, the same as if we don't have the restriction.

@ljharb
Copy link
Author

ljharb commented Jan 7, 2019

oh sure, i ts not like restricting this to one call prevents multiple calls - a dev can always construct classes in a loop and instantiate multiple objects. It’s more like, super() can only be called once, so what’s the use case for calling this more than once? How likely is it that calling it more than once is a bug, versus being intentional? If the intentional use case is rare enough, and still possible just with more boilerplate, and the buggy case is more common, then it seems beneficial to ensure it’s only called once (also, that you can’t both call it, and super(), on the same instance)

@jridgewell
Copy link

jridgewell commented Feb 1, 2019

Note that if any fields are private, PrivateFieldAdd (which defines the field on the object) will throw on second attempt (test262).

@ljharb
Copy link
Author

ljharb commented Feb 1, 2019

That’s good to know; i was also thinking of preventing it from being called on two different objects.

@littledan
Copy link
Owner

I'm not sure what sort of invariants it would provide to prevent new.initialize from being called on multiple objects. Without this feature, a constructor may recurse, and so in conjunction with the super return trick, it could (contortedly) achieve the same effect.

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

3 participants