How to choose between private member and let binding?

后端 未结 3 1828
陌清茗
陌清茗 2021-01-17 21:29

When writing a private method that has no need to access other members of the same class, how do you choose between private member and let binding?

  • I tend to u
3条回答
  •  庸人自扰
    2021-01-17 22:07

    The relevant portion of the spec is section 8.6.2. It states:

    The compiled representation used for values declared in “let” bindings in classes is either:

    • A value that is local to the object constructor (if the value is not a syntactic function, is not mutable and is not used in any function or member).

    • An instance field in the corresponding CLI type (if the value is not a syntactic function, but is used in some function or member).

    • A member of the corresponding CLI type (if the value is a syntactic function).

    Also:

    Non-function let-bindings that are not used in either the members of a type or a function binding are optimized away and become values that are local to the resulting CLI constructor. Likewise, function bindings are represented as instance members.

    I prefer let bindings to private members because they're more "functional," i.e., they emphasize "what" over "how." The compiler takes care of the optimal compiled form.

提交回复
热议问题