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
the "super" object you're talking about is the object
Object with the object prototypeyeah heard of that, basically it has default properties/methods, like
toString() and so on ?however, you can create an object without a prototype
you can use
Object.createObject(null)
yesoh ok
but the thing is
why is the prototype useful or why might we remove/create an object without the prototype ?
a good reason to create an object without prototype is because you don't want the object prototype
why? that's situational
what does a prototype allow us to do? Just to access in-built things like
toString() ?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
yeah basically, object inheriting from other objects?
yup, from other objects' prototypes
it is very weird, i know
by the way, classes are just prototypes, but pretty
yeah I need to read a bit on that
and experiment
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
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.ohh ok
interesting
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 existedAnd how other polyfills work.
basically
with core-js, it's a tiny bit fancier, but it is essentially that