Object prototype in JS

Hello, I was reading a bit about object prototype. From what I've understood, it's just a chain where objects kind of inherit the properties of other objects. So basically, every objects we create already have some properties, so I guess we have a "super" object or something like that from which all the objects "inherit" from? Why is this prototype chain important and is object prototype useful/important?
16 Replies
ἔρως
ἔρως7mo ago
the "super" object you're talking about is the object Object with the object prototype
Faker
FakerOP7mo ago
yeah heard of that, basically it has default properties/methods, like toString() and so on ?
ἔρως
ἔρως7mo ago
however, you can create an object without a prototype you can use Object.createObject(null) yes
Faker
FakerOP7mo ago
oh ok but the thing is why is the prototype useful or why might we remove/create an object without the prototype ?
ἔρως
ἔρως7mo ago
a good reason to create an object without prototype is because you don't want the object prototype why? that's situational
Faker
FakerOP7mo ago
what does a prototype allow us to do? Just to access in-built things like toString() ?
ἔρως
ἔρως7mo ago
no it's the prototype chain for a reason the prototype will have the methods and some global properties and those are passed on for each object that has the prototype it's a weird way of doing inheritance
Faker
FakerOP7mo ago
yeah basically, object inheriting from other objects?
ἔρως
ἔρως7mo ago
yup, from other objects' prototypes it is very weird, i know by the way, classes are just prototypes, but pretty
Faker
FakerOP7mo ago
yeah I need to read a bit on that and experiment
ἔρως
ἔρως7mo ago
one thing about the prototype: you don't need to re-define the functions for each time you create the object the object just magically gets the methods and properties
13eck
13eck7mo ago
Back in the day, when JS was created. The average computer had around 2–4MB of RAM. MB of RAM. Let that sink in. And JS objects weren't that memory efficient yet, b/c it was brand new. What the prototype chain allows for is to have one copy of a function and every object that inherited from the base object could access that function. Imagine, for a moment, that you had a site with a dozen buttons. With only 2MB of RAM you couldn't have 12 different copies of the same .addEventListener() function…you'd crash their machine (remember that 2MB is shared among all programs running)! So instead you have one copy that all buttons use. Now think about if you wanted to create your own button. Without prototype inheritance, you'd have to re-invent the wheel by implementing all those functions yourself. Or you could inherit from the ButtonElement and BOOM! You have everything already. Another benefit to the prototype chain is that any change on one object will affect all others that inherit from it. So if you wanted to add a new Array method, you'd just monkey-patch Array.prototype.myHack() and now all arrays have myHack availble to them.
Faker
FakerOP7mo ago
ohh ok interesting
ἔρως
ἔρως7mo ago
i didn't want to go into the ram because i would over over overcomplicate but this is perfectly spot on that's how we got things like forEach before forEach ever existed
13eck
13eck7mo ago
And how other polyfills work.
if (!Array.prototype.forEach) {
Array.prototype.forEach = function() {}
}
if (!Array.prototype.forEach) {
Array.prototype.forEach = function() {}
}
ἔρως
ἔρως7mo ago
basically with core-js, it's a tiny bit fancier, but it is essentially that

Did you find this page helpful?