Skip to content

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

Open

Description

@ljharb
No description provided.

Activity

  1. littledan commented on Jan 6, 2019

    @littledan
    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...)

  2. ljharb commented on Jan 7, 2019

    @ljharb
    Author

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

  3. littledan commented on Jan 7, 2019

    @littledan
    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.

  4. ljharb commented on Jan 7, 2019

    @ljharb
    Author

    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)

  5. jridgewell commented on Feb 1, 2019

    @jridgewell

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

  6. ljharb commented on Feb 1, 2019

    @ljharb
    Author

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

  7. littledan commented on Feb 4, 2019

    @littledan
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions