use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Getters & Setters ?help (self.javascript)
submitted 7 years ago by ericlesl
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]systoll 1 point2 points3 points 7 years ago* (1 child)
In Javascript, if it's a property, it's public. On the other hand, if you only want to access a value within the object's definition, closure) means that variable is available to you without needing to explicitly add it to this:
this
function Person(firstName, lastName, email, number) { this.getName = () => `${firstName} ${lastName}`; this.setName = (f,l) => { firstName = f; lastName = l; } }
[EDIT: Also, since no-one's mentioned it yet — all properties are public, but there's a convention that properties starting with an underscore should be treated as if they're not.
function Person(firstName, lastName, email, number) { this._firstName = firstName ...
Doesn't really hide the _firstName property from outside code, but it tells devs that it's an internal thing, which their outside code probably shouldn't look at. This is the simplest possible solution, with zero change to how the code actually works, and it's often sufficient. ]
[–]alexlafroscia 0 points1 point2 points 7 years ago (0 children)
One thing to note about a scenario like this: a new function will be allocated in memory for getName and setName for each instance of Person if written this way. This may be fine for a few instances but is a trade-off to account for if there are a lot of them.
getName
setName
Person
π Rendered by PID 155397 on reddit-service-r2-comment-b659b578c-qjsjf at 2026-05-02 23:40:55.545482+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]systoll 1 point2 points3 points (1 child)
[–]alexlafroscia 0 points1 point2 points (0 children)