Skip to content

Question regarding your usage of a getter #274

Description

@stela5

I'm not a JavaScript expert so I could be totally wrong on this, but for the http://www.polymer-project.org/polymer.html docs page, instead of

get foo() {
  return foo_;
}

wouldn't it be clearer to use

getFoo: function() {
  return this.foo_;
}

See:
http://jtfreeman.blogspot.com/2013/07/javascript-getters-and-setters-last.html
http://jsperf.com/es5-getters-setters-versus-getter-setter-methods/10

Activity

  1. sjmiles commented on Sep 13, 2013

    @sjmiles
    Contributor

    Your question is confusing because your examples are not semantically equivalent.

    To access foo with the first (getter) version you can just do obj.foo. This is a property getter.

    To access foo the second way requires obj.getFoo(), which is a functional interface.

  2. stela5 commented on Sep 13, 2013

    @stela5
    Author

    Sorry, I should have been more explicit in my question. I was really probing into why the author preferred a getter (and by implication, setter) approach rather than a more common functional interface such as

    obj.getFoo()

    In other words, I was using the example as an excuse to engage in a broader discussion about the proper use cases for getters and setters. I've read the documentation at to how to use them but, as Jonathan Freeman pointed out, I'm not sure as to when or why to use them. Thanks in advance for any insights.

  3. sjmiles commented on Sep 13, 2013

    @sjmiles
    Contributor

    The only reason the docs page uses this pattern is as an example for how to
    declare private data (_foo).

    IMHO, the general reason to use getters and setters is to unify the
    modality of accessors.

    If foo is a simple property, it's natural to write:

    this.foo += 3;

    It's useful to programmers to be able to imbue foo with underlying
    abilities without the cognitive load of a new syntax. IOW, a foo
    getter/setter accessor pair will still work with the above construction.
    However, given a functional interface, now you must write:

    this.setFoo(this.getFoo() + 3);

    Also, unless you got 100% to functional interfaces, the user must
    distinguish between flavors of properties. E.g. perhaps you must use
    this.getFoo() to get foo but this.bar to get bar.

    Ultimately these are subjective differences. My personal taste runs towards
    simplifying APIs and hiding complexity. Other folks (justifiably) put more
    emphasis on being explict. Salt to taste.

    On Fri, Sep 13, 2013 at 8:39 AM, stela5 notifications@github.com wrote:

    Sorry, I should have been more explicit in my question. I was really
    probing into why the author preferred a getter (and by implication, setter)
    approach rather than a more common functional interface such as

    obj.getFoo()

    In other words, I was using the example as an excuse to engage in a
    broader discussion about the proper use cases for getters and setters. I've
    read the documentation at to how to use them but, as Jonathan Freeman
    pointed out, I'm not sure as to when or why to use them. Thanks in
    advance for any insights.

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/274#issuecomment-24403247
    .

  4. stela5 commented on Sep 20, 2013

    @stela5
    Author

    Thanks!

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