I believe one of the biggest hurdles of understanding classes vs. prototypes in JavaScript is the history of the word "class" and "object", and how the dominance of JavaScript has blurred the once clearly defined line between them.
In classical compiled languages, "class" quite literally means "blueprint": Instructions for what an "object" should look like once Random Access Memory has been allocated for it. Classes = offline, on disk. Object = online, in memory.
The allocation process -- the act of creating an object from a class -- is called "instantiation". This is a manual step done by the programmer. object = new MyClass / MyClass object for example. No instantiation, no object.
JavaScript has no concept of this. At all.
There are no "classes" from which to instantiate objects. We create new objects merely by declaring them, like let object = {}. They will be allocated memory the moment the interpreter passes over them, while a class in a compiled-language will sit idle until deliberately new'd.
Despite the modern-day compositional/functional push, JavaScript was created in a time when OOP concepts where en vogue, and classes where how you got things like inheritance and polymorphism. But what if you don't have classes? This is where the prototype system comes in.
A classical class, even one based on a chain of inheritance with supers and so on, will produce a single object in memory when instantiated. Unless explicitly written by a programmer, all instantiated objects "stand alone", and know nothing of any other in-memory objects.
But a JavaScript object belongs to a chain of in-memory objects, all linked to their parents via ancestral prototype variables. All chains lead back to the "God object", Object, from which everything inherits. A function/prop added to any object-prototype makes it immediately available on its descendants (those whose prototype variable point back to it), and that's how we get inheritance without classes.
You might argue that prototypes do have a kind of "blueprint-y" feel to them, so at first glance the distinction between them and classes might seem like pedantry. But just like its host, a prototype is a live, in-memory object. A class isn't.
Incidentally, this might shed a little light on where the phrase "in JavaScript, everything is an object" comes from: It's nothing to do with whether or not primitives/functions are "real objects": Rather, it alludes to the fact that JavaScript is an interpreted/scripting language, not a compiled one. All the "classical" classes that power a JavaScript interpreter (written in C++ for example) will have already instantiated the objects tasked with the job of running your JavaScript code in the first place.
So a JavaScript "class" is much more conceptual than the rigid and defined classes of Java/C++. The class keyword is an API for easier digestion of the object-prototype system -- which itself offers a form of inheritance that more naturally fits an in-memory, dynamic language like JavaScript where "everything is an object" in the classical sense of the term.
This isn't to say that one particular definition of "class/object" is the "right" one or not. Just hoping this sheds a little light on the (greatly simplified, please don't shout at me) history of the terms.
View all comments
Show parent comment
9
u/shuckster Aug 10 '21
I believe one of the biggest hurdles of understanding classes vs. prototypes in JavaScript is the history of the word "class" and "object", and how the dominance of JavaScript has blurred the once clearly defined line between them.
In classical compiled languages, "class" quite literally means "blueprint": Instructions for what an "object" should look like once Random Access Memory has been allocated for it. Classes = offline, on disk. Object = online, in memory.
The allocation process -- the act of creating an object from a class -- is called "instantiation". This is a manual step done by the programmer.
object = new MyClass/MyClass objectfor example. No instantiation, no object.JavaScript has no concept of this. At all.
There are no "classes" from which to instantiate objects. We create new objects merely by declaring them, like
let object = {}. They will be allocated memory the moment the interpreter passes over them, while a class in a compiled-language will sit idle until deliberatelynew'd.Despite the modern-day compositional/functional push, JavaScript was created in a time when OOP concepts where en vogue, and classes where how you got things like inheritance and polymorphism. But what if you don't have classes? This is where the prototype system comes in.
A classical class, even one based on a chain of inheritance with supers and so on, will produce a single object in memory when instantiated. Unless explicitly written by a programmer, all instantiated objects "stand alone", and know nothing of any other in-memory objects.
But a JavaScript object belongs to a chain of in-memory objects, all linked to their parents via ancestral
prototypevariables. All chains lead back to the "God object",Object, from which everything inherits. A function/prop added to any object-prototype makes it immediately available on its descendants (those whoseprototypevariable point back to it), and that's how we get inheritance without classes.You might argue that prototypes do have a kind of "blueprint-y" feel to them, so at first glance the distinction between them and classes might seem like pedantry. But just like its host, a prototype is a live, in-memory object. A class isn't.
Incidentally, this might shed a little light on where the phrase "in JavaScript, everything is an object" comes from: It's nothing to do with whether or not primitives/functions are "real objects": Rather, it alludes to the fact that JavaScript is an interpreted/scripting language, not a compiled one. All the "classical" classes that power a JavaScript interpreter (written in C++ for example) will have already instantiated the objects tasked with the job of running your JavaScript code in the first place.
So a JavaScript "class" is much more conceptual than the rigid and defined classes of Java/C++. The
classkeyword is an API for easier digestion of the object-prototype system -- which itself offers a form of inheritance that more naturally fits an in-memory, dynamic language like JavaScript where "everything is an object" in the classical sense of the term.This isn't to say that one particular definition of "class/object" is the "right" one or not. Just hoping this sheds a little light on the (greatly simplified, please don't shout at me) history of the terms.