Repository navigation
Question regarding your usage of a getter #274
Description
Activity
Your question is confusing because your examples are not semantically equivalent.
To access
foowith the first (getter) version you can just doobj.foo. This is a property getter.To access
foothe second way requiresobj.getFoo(), which is a functional interface.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.
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
foois a simple property, it's natural to write:this.foo += 3;
It's useful to programmers to be able to imbue
foowith 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 getfoobut this.bar to getbar.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 asobj.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
.Thanks!
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