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.

Prototypes Are Not Classes

64 pointsby raganwaldover 11 years ago

11 comments

kyberiasover 11 years ago
The author has to emphasize that prototypes and classes are a different concept at least five times in the article. Why? Because the explanation is long-winding and weak.
评论 #7086385 未加载
评论 #7087507 未加载
评论 #7086519 未加载
roel_vover 11 years ago
Every time someone tries to explain why prototypes are so very much not classes, I can&#x27;t help but come away thinking &#x27;yeah potato&#x2F;potato - so they&#x27;re basically the same&#x27;. Be it Crockford, this guy, any of the 100 articles you find only when you go looking for it. Sure the nuances between languages are different, big deal - what is called &#x27;class&#x27; in a dynamic language is very different from what is called &#x27;class&#x27; in C++, yet everybody calls them classes and doesn&#x27;t go around writing articles about how runtime-modifiable object blueprints <i>really</i> are fundamentally different from those that are set in compile time.
评论 #7086375 未加载
rtpgover 11 years ago
This sort of realization is the first step to realizing that you really shouldn&#x27;t be writing javascript like java&#x2F;C++. It makes things feel a lot nicer when you start really thinking in JS. I&#x27;d recommend checking out Crockford&#x27;s &quot;Crockford on Javascript&quot; series for some good intro on thinking about Javascript (<a href="http://www.youtube.com/watch?v=JxAXlJEmNMg" rel="nofollow">http:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=JxAXlJEmNMg</a>). Section 3 is where you really start talking about JS.<p>For a good counter-example, check out a lot of google&#x27;s JS libs (in all their giant module goodness).
Jareover 11 years ago
I&#x27;ve never heard anyone, experienced or newbie, say &quot;JavaScript has classes&quot;. People at best will say &quot;You can sort of make classes in JavaScript using objects&quot;.<p>To me a Class has always been &quot;a family of objects with the same structure and behaviour, and a way to create them&quot;. Stuff like hiding, constructors, type checking, polymorphism and inheritance are interesting, but secondary details. But then, I was taught OOP using Modula-2 back in &#x27;89, so I&#x27;m used to the idea that language features and paradigm features need not match 1:1
评论 #7088761 未加载
lispmover 11 years ago
Jesus, the language is called Smalltalk. Not SmallTalk.<p>Btw., in a typical language supporting prototypes, I would expect that prototypes are nothing special. Every object could be a prototype for others...
评论 #7085807 未加载
评论 #7085968 未加载
CrossEyeover 11 years ago
I think there is a serious problem in vocabulary. Wikipedia&#x27;s definition of class ([1]) is quite reasonable, which, slightly paraphrased, reads, &quot;A class is an extensible template for creating objects, providing initial values for state and implementations of behavior.&quot; While typically we don&#x27;t use prototype objects for default state in Javascript, we always <i></i>can<i></i> if we choose, and they are widely used for providing behavior. And both &quot;extensible&quot; and &quot;template&quot; fit well. So by this definitions, prototypes really are classes.<p>But almost everyone who comes to Javascript from a class-based language brings a number of additional preconceptions. I don&#x27;t know the Ruby community well enough to know if your descriptions are accurate, but I know that many of those coming from Java or C# backgrounds expect classes to be (1) distinct creatures from objects (and those who know much about the reflection class Class understand that the objects of that class are objects and not the classes themselves), (2) design&#x2F;development&#x2F;compile-time abstractions separate from the run-time realities, (3) unchanging at runtime (which is obviously different from Ruby), and (4) the enforcers of access policies of instance methods. None of these things are true for Javascript prototypes. So to these people, it&#x27;s easiest to explain that Javascript does not have classes.<p>I have never implemented a serious programming language. But I understand from those who have that implementing classes and implementing prototypes can be very, very similar processes. So, while I won&#x27;t stop insisting to those from other languages that Javascript doens&#x27;t have classes, I do recognize that I&#x27;m talking more to their preconceptions and not to the deepest truth I know.<p><pre><code> [1]: http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Class_(computer_programming)</code></pre>
pjmlpover 11 years ago
I guess before learning JavaScript, developers need to be sent to a Self learning camp.
评论 #7085512 未加载
评论 #7085222 未加载
mlexover 11 years ago
I hate to be _that_ guy, but I found the grey on light-grey text to be difficult to read.
egilover 11 years ago
Yet another shallow article describing the difference between prototypes and classes. Does anybody know of more in depth ones that also discusses multi-leven inheritance, polymorphism etc?
评论 #7089759 未加载
CmonDevover 11 years ago
Good (enough) for GUIs, bad for business logic.
ossdev1over 11 years ago
Never get why people are so excited with atomization, that necessity of decomposing everything in atomic parts...
评论 #7084881 未加载
评论 #7085337 未加载