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

Question regarding your usage of a getter #274

Closed
stela5 opened this issue Sep 13, 2013 · 4 comments
Closed

Question regarding your usage of a getter #274

stela5 opened this issue Sep 13, 2013 · 4 comments

Comments

@stela5
Copy link

stela5 commented Sep 13, 2013

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

@sjmiles
Copy link
Contributor

sjmiles commented Sep 13, 2013

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.

@stela5
Copy link
Author

stela5 commented Sep 13, 2013

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.

@sjmiles
Copy link
Contributor

sjmiles commented Sep 13, 2013

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
.

@stela5
Copy link
Author

stela5 commented Sep 20, 2013

Thanks!

@stela5 stela5 closed this as completed Sep 20, 2013
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

2 participants