Skip to content

initializing object or array properties using init: do we still need this with native classes? #566

Closed

Description

In customizing headers section, we state:

Note that we set headers in init() because default property values should not be arrays or objects.

I think this prohibition is about avoiding leaking state.
Wouldn't using native classes supersede this implementation quirk?
If so, what is the recommended way to set those properties?

Activity

  1. ghost changed the title [-]initializing complex properties using init: do we still need this with native classes?[/-] [+]initializing object or array properties using init: do we still need this with native classes?[/+] on Mar 6, 2019
  2. locks commented on Mar 6, 2019

    @locks
    Contributor

    The two ways to initialize properties in native classes is in constructors or field initializers (class A { propName = [1, 2, 3] }).
    However, since field initializers are still in stage 3 I would keep the constructor recommendation.

    P.S. with native classes that extend from EmberObject, you should use init and not a constructor.

  3. pzuraq commented on Mar 6, 2019

    @pzuraq
    Contributor

    We should use class fields for this case, and stop teaching folks to initialize in constructor/init unless it's some fairly complicated initialization work.

    As for init vs constructor, the gist is that if your parent class does not use init (or other EmberObject APIs), you're safe using constructor. Based on a quick look at the code for adapters/serializers/models, and our conversation with @runspired, I think it should be safe to use constructor in examples, but we should definitely prefer class fields.

  4. added a commit that references this issue on Mar 13, 2019
    059cb8c
  5. ghost closed this as completedon Mar 13, 2019
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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions