TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Encapsulation in Javascript

24 pointsby Aviwein77over 13 years ago
Thought that it was interesting and others might enjoy it as well.

3 comments

jashkenasover 13 years ago
Not to be hyperbolic, but the continual promotion of the "module" pattern in JavaScript is some of the worst advice you can give.<p>JavaScript has prototypes for a reason -- use them. By using the "module" pattern to build objects, you create a separate copy of every function for every instance of every object you create. If you're just creating a handful of objects, it's no big deal, but if you're creating a large number of objects, it's horribly CPU and memory inefficient.<p>Modern JS runtimes like Chrome/V8 can create and store a million small objects with prototypes and "new" in a couple seconds, using just a dozen or so megabytes of RAM. Creating the same million small objects with the "module" pattern takes minutes, uses many hundreds of megabytes of memory, and often crashes the browser.<p>And that's just the pragmatics -- there are deeper semantic reasons to use real prototypes.
评论 #2992502 未加载
评论 #2991972 未加载
评论 #2992120 未加载
评论 #2992128 未加载
评论 #2992123 未加载
latchkeyover 13 years ago
CoffeeScript FTW. <a href="https://gist.github.com/1214296" rel="nofollow">https://gist.github.com/1214296</a>
primo44over 13 years ago
"Module's are where it's at"?<p>Grammar much?<p>And don't get me started on "login with X, Y, Z". As a verb, it's "log in". YC has it wrong also.
评论 #2992995 未加载