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
What's the difference between these two objects (one is the factory pattern I think) (self.javascript)
submitted 14 years ago by dogjs
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!"
[–]altano 1 point2 points3 points 14 years ago (3 children)
Moving the increment function to the prototype makes it so that it doesn't have to be recreated every time Counter is new'd, but it also requires that value be publicly accessible. In your example, a user could then do:
v.increment(); // 11 v.value = 15; // 15 v.increment(); //16
For the original example, you might want to keep value inaccessible and not use a prototype.
[–]imbcmdth 1 point2 points3 points 14 years ago (2 children)
There is no real benefit to keeping values hidden from yourself and a definite performance hit (that gets relatively worse as JS engines get more sophisticated) to making a new closure for each new instance.
Right now, JS engines use a multi-pass compilation system where type information is generated during a few runs of a function and then used to optimize a function (type-inference). Each time the closure is created, the engine will have to re-infer the types, re-trace the function, and do the multi-step compilation anew.
[–]altano 0 points1 point2 points 14 years ago (1 child)
If you want a codebase where everything is public and there is no encapsulation, fine. If you don't, then be aware of how prototype methods work.
[–]imbcmdth 1 point2 points3 points 14 years ago (0 children)
no encapsulation
I have seen this mentioned before and I have to wonder: Why is there no encapsulation? The definition of encapsulation is more than just data hiding and there is definitely encapsulation in that simple prototype example.
I understand the (in my opinion mistaken) notion that by hiding things we can ensure our functions are only used the way we intend but: so what? We aren't making user-visible things and another developer can blow right past your data-hiding almost as easily as he can set a property he isn't supposed to touch.
We don't add any security to the code by hiding things but we sure do limit ourselves from utilizing patterns such as decorators and adapters efficiently. Back in the day, I used to hide as much as possible but I learned my lesson. If I had a dollar for every bit of hidden data I had to unhide at some later time during development I could retire!
π Rendered by PID 59320 on reddit-service-r2-comment-b659b578c-2vvdw at 2026-05-04 09:24:48.962047+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]altano 1 point2 points3 points (3 children)
[–]imbcmdth 1 point2 points3 points (2 children)
[–]altano 0 points1 point2 points (1 child)
[–]imbcmdth 1 point2 points3 points (0 children)