Why not use JavaScript getters?
See original GitHub issueI’m not sure if I’m overlooking something or if this is a stupid question, but… Why not use JavaScript getters?
JavaScript objects have support for getters: (works everywhere except on IE8)
- http://ejohn.org/blog/javascript-getters-and-setters/
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects
Why not use them (optionally) instead of .get()
and .getIn()
? Especially for server-side code and for applications that aren’t focusing IE8 this could be really helpful.
Issue Analytics
- State:
- Created 9 years ago
- Reactions:5
- Comments:11
Top Results From Across the Web
why should not use getters/setters? #1880 - GitHub
Getters and setters are intended to hide computational functionality. They are made specifically so you wouldn't know if the value is computed.
Read more >Are `getter` and `setter` necessary in JavaScript?
A getter has use when you want to run some code every time a property is requested. In your example, the getter always...
Read more >Be careful with JavaScript getters and setters | by Kesk
Since you reference the property person.age, JavaScript needs to get that property. When this happens, the getter is triggered. JavaScript calls the getter,...
Read more >No, Getters and Setters in TypeScript & JavaScript aren't useless
In this blog post, we talk about the utility of getters and setters in modern web development. Are they useless? When does it...
Read more >Why getters/setters is a bad idea in JavaScript
You're right that detecting typos is a problem, but the cause isn't setters and getters – it's Javascript's lack of static types. If...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
FWIW,
Object.defineProperty
is now just as fast as getting/setting via methods. https://jsperf.com/getter-setter/8@JannesMeyer AFAIK they are terribly slow. I saw a benchmark (can’t find the link at the moment) and they were 10 times slower than usual approach.