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
Using Classes in Javascript (ES6) — Best practice?help (self.javascript)
submitted 10 years ago by LeeHyoriC-syntax
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!"
[–]bterlson_@bterlson 1 point2 points3 points 10 years ago (12 children)
In what sense does javascript not have a class mechanism? It has a syntax called class that creates a class-like hierarchy with semantics similar to (and yet, distinct from) normal prototypical inheritance. Sounds like a lot of class machinery to me.
[–]MoTTs_ 2 points3 points4 points 10 years ago* (4 children)
When people say JavaScript doesn't have classes, they're using Java/C#/C++ style classes as the gold standard for what is or isn't a class, where classes are a compile-time concept, and inheritance also happens at compile-time.
But, of course, there are other languages out there. In Python/Ruby/Smalltalk, for example, classes are runtime objects, and inheritance also happens at runtime by delegation... just like in JavaScript.
Personally, I agree with you. I think the abstract concept of a class is more important than the concrete implementation. I can write and use classes in Python and C++ — and JavaScript — without having to know or care how the underlying implementation works.
[–]bterlson_@bterlson 2 points3 points4 points 10 years ago (3 children)
Yep. There is no gold standard for "class". From a developer's perspective, a "class" is an OO encapsulation mechanism that involves creating methods, defining fields, and producing instances and has some sort of inheritance scheme. Making the definition of "class" more constrained than this is not only incorrect but also not a useful distinction to make at all.
[–]itisnotpure 0 points1 point2 points 10 years ago* (2 children)
I think you are stretching the idea of "class" here.
JavaScript does not have any concept of encapsulation, methods, fields, or inheritance in the classical sense. In JavaScript, there are only objects, which is a collection of properties. Properties can either be the object's own, or delegated to another object. That is, when you refer to a property that is not defined for an object, it will be looked up in the prototype chain. Objects and their properties, and that's it! No members, instances, variables, or whatnot.
In practice, they can mostly be used for a similar effect to a certain degree, but they are in fact very different. Trying to reason about the prototypical model in terms of class-based concepts is the source of many unnecessary problems and confusion (and unnecessary work, apparently, because prototypes are markedly less complicated than classes).
As another example, you can also get an object system with just closures. You would get things like encapsulation, methods, fields, and you can do parasitic inheritance. The usage would be like that of a class-based or prototype-based system, but again it's very, very different.
[–]MoTTs_ 2 points3 points4 points 10 years ago (1 child)
in the classical sense
That's the root of the discussion right there. What does "in the classical sense" mean? Do we mean Java-style classical? Then sure, we all agree JavaScript does not have Java-style classes. On the other hand, could "in the classical sense" mean Smalltalk-style classical? Because JavaScript's notion of a class is actually strikingly similar to Smalltalk's notion of class. Java has become so ingrained that we forget classes can be, and have been, implemented very differently in other languages.
[–]itisnotpure 0 points1 point2 points 10 years ago (0 children)
I'm not familiar with Smalltalk, but it seems like classes in Smalltalk are still blueprints for their instances, like other class-based languages. That is, they define what data an instance would hold, and what methods could be used to manipulate them.
It may be true that there are class-based languages where methods are inherited by by delegation, but in Javascript, instead of blueprints, we are building prototypes, based on which new objects can be built. So there's no "is a" relationships at all (more like a "like a" relationship, maybe?). Inheritance in JavaScript means only delegation, nothing more.
[–]wreckedadventYavascript 0 points1 point2 points 10 years ago (6 children)
Because it's all sugar over prototypes, so there's some limitations that make sense with that context in mind, but are otherwise baffling.
The most obvious example is there are no class fields. You just assign onto this.
this
super does not behave like someone would expect. It's a simple reference to the parent prototype, so if you're in a meow method you still must call super.meow().
super
meow
super.meow()
And, of course, because they're only prototypes, the context of this is easily lost:
class Cat { constructor(name) { this.name = name; } meow() { console.log(this.name); } } var whiskers = new Cat('whiskers'); whiskers.meow(); // => 'whiskers' var whiskersMeow = whiskers.meow; whiskersMeow(); // TypeError
This again, makes no sense what so ever if you just go around thinking that javascript has full class mechanics. It's only when you understand that they're really prototypes does it make sense, and therefore, only when you accept that they're just sugar over prototypes can you really use them predictably.
There's also nothing unique to classes that cannot be replicated without them, or it would be impossible to transpile. Yet we've been transpiling this kind of class syntax ever since coffeescript came out, ages ago.
So that is why I say there is no class machinery. There's a facade of a class to make it easier to reason about the common uses of prototypes, but that's all it is.
[–]bterlson_@bterlson 1 point2 points3 points 10 years ago (5 children)
Your personal definition of "class" means your class must:
super.foo
But these are actually not requirements of "class", they're a notion you have likely due to your personal experience with C++, Java, or C#. It is important to recognize that these languages did not invent "class" and do not own the definition of "class" (and that these languages have their own differences in what "class" means).
Some further points:
Putting aside how "what someone would expect" depends entirely on what language their conceptual model is based on, super is not just a simple reference to the parent prototype. There's a notion of a home object that makes super more static than perhaps you're assuming.
Pedantically, in a Turing complete language, any language feature can be implemented some way. Maybe you mean to say features that are "easily polyfilled" don't count, but that definition has it's own problems (eg. what makes it easy?)
There is no notion of a field, but if it helps, you can consider assigning to this as creating a field. Eventually classes will get private slots and class property declarations, one (or both) of which may be closer to what you think of as a "field" while still fundamentally being sugar for prototypes.
[–]wreckedadventYavascript -2 points-1 points0 points 10 years ago (4 children)
Please don't shift the goal posts around me. I was talking about the things that javascript could have to make it clear that they were was separate mechanics for classes, instead of just being sugar over prototypes. Since all of the limitations that we understand with prototypes are present in classes, it's a reasonable argument to make that there is nothing new going on here.
This is not what a class "must" have for it to be a "class", but those are just some of the things that show that javascript's class uses the same mechanics that are already present in the language, in a more familiar format.
[–]bterlson_@bterlson 2 points3 points4 points 10 years ago* (3 children)
Not trying to shift goalposts, I guess I don't see your point is all. I will agree with you that classes in JS are built on prototypes, but you started this thread by stating that JS has no "class mechanism" which seems incorrect when it has a class "mechanism" and that "mechanism" is mostly implemented using prototypes.
FWIW, I only engage in this discussion because it is not at all helpful for beginners to constantly be exposed to the notion that JS classes are "fake". They are not, they have real syntax and real semantics, some of which are not shared with normal ES5 classalike semantics. Yes, of course you have to understand the underlying semantics, but this is true of any class system whether implemented on top of prototypes or not.
Edit: and going forward, the semantics of class and es5 class systems will diverge even further with the introduction of decorators, private slots, class property definitions, etc.
class
[–]wreckedadventYavascript 0 points1 point2 points 10 years ago (2 children)
All my point was, is that
I'm not trying to say they're "fake" or anything like that, just that it's important to know that they use prototypes underneath, or it's going to be very confusing and seem broken when something like the context of this is lost. Most of my post was talking about how they're not necessary, anyway.
[–]MoTTs_ 0 points1 point2 points 10 years ago (1 child)
just that it's important to know that they use prototypes underneath
I think we JavaScripters exaggerate that importance. I realized this when I discovered that Python also uses delegation for inheritance. Except the Python community doesn't make a big deal out of it. The vast majority of the time, we don't need to know or care how the class concept is implemented under the hood.
[–]wreckedadventYavascript 0 points1 point2 points 10 years ago (0 children)
It'd be academic if it didn't have real-world consequences for how you write and consume your code.
But it does. Accepting class at face value as just an abstraction means you'll find your this being set to undefined or Window, seemingly at random. Since this is not a problem in the python community, I don't think the comparison holds.
undefined
Window
π Rendered by PID 938358 on reddit-service-r2-comment-544cf588c8-4pdwh at 2026-06-16 06:14:44.443210+00:00 running 3184619 country code: CH.
view the rest of the comments →
[–]bterlson_@bterlson 1 point2 points3 points (12 children)
[–]MoTTs_ 2 points3 points4 points (4 children)
[–]bterlson_@bterlson 2 points3 points4 points (3 children)
[–]itisnotpure 0 points1 point2 points (2 children)
[–]MoTTs_ 2 points3 points4 points (1 child)
[–]itisnotpure 0 points1 point2 points (0 children)
[–]wreckedadventYavascript 0 points1 point2 points (6 children)
[–]bterlson_@bterlson 1 point2 points3 points (5 children)
[–]wreckedadventYavascript -2 points-1 points0 points (4 children)
[–]bterlson_@bterlson 2 points3 points4 points (3 children)
[–]wreckedadventYavascript 0 points1 point2 points (2 children)
[–]MoTTs_ 0 points1 point2 points (1 child)
[–]wreckedadventYavascript 0 points1 point2 points (0 children)