5 ms·
I'm pretty much aware about prototype inheritance in js. But still scratching my head everytime, I try to understand difference between prototype and __proto__.
by finchisko 8y ago
I'm pretty much aware about prototype inheritance in js. But still scratching my head everytime, I try to understand difference between prototype and __proto__. Thankfully with ES6 classes and class inheritance constructs, we don't need to override prototype constructor and such a things anymore. Still wish, that somebody will explain it to me in plain english.
- sophiebits 8y agoThis post does try to explain it in plain English. See the explanation starting at the bolded sentence that starts "Confusingly,".
- ricardobeat 8y agoHonestly I don’t think it does a good job at explaining it. Classes are mostly syntax sugar for “old” constructors and prototypes, not the other way around (that’s the whole reason you can’t tell them apart). Would be easier to explain it that way; on mobile right now so can’t comment too much.
- jrumbut 8y agoI thought it was unfortunate that prototypes were characterized as an inadequate approximation of classes, when in fact prototypes are a wholly legitimate, powerful, and certainly interesting way to approach object-oriented programming (https://en.m.wikipedia.org/wiki/Prototype-based_programming https://en.m.wikipedia.org/wiki/Prototype-based_programming). 20+ years of confused programmers suggests that maybe it wasn't the right way to go for a language that is frequently used by beginners, but still that wasn't the fault of prototype based programming itself. The new class system is syntactic sugar leveraging the power of prototypes. This was always possible, and nothing stopped you from implementing a class-like system in JavaScript a long time ago. More, very readable, info here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Details_of_the_Object_Model https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guid...
- danabramov 8y agoI tried not to mention classes too explicitly in the section that deals with __proto__, and only explain the prototypes themselves. But keep in mind that new readers are often exposed to classes only so they need some practical ground. In any case there's plenty of resources -- maybe my explanation will click with somebody.
- pests 8y ago.__proto__ is that objects prototype that was used to create it. .prototype is the template of newly created objects of the object you are inspecting.
- deleted 8y ago[deleted]