DEV Community

Cover image for A Beginner's Guide to JavaScript's Prototype

A Beginner's Guide to JavaScript's Prototype

Tyler McGinnis on October 09, 2018

You can’t get very far in JavaScript without dealing with objects. They’re foundational to almost every aspect of the JavaScript programming langua...
Collapse
 
andrewdtanner profile image
Andrew Tanner 🇪🇺

This is a great and thorough explanation. I wish I could flex my JS more but I just don't get the opportunities. All I can do is read articles nlike this and hope it stays in my head until it might be needed.

Collapse
 
codnificent profile image
Kingsley Aguchibe

Funny enough but true

Collapse
 
differentsmoke profile image
Pablo Barría Urenda • Edited

I feel like I just watched a Douglas Crockford lecture in reverse!!

This is a great explanation, but just to weigh in from Crockford's perspective, who's very skeptic about both class and new:

  • Historically, prototypal inheritance was meant to be an improvement of classical inheritance. So in his view the addition of class to JS is actually a step backwards. In my view, it is a misnomer because you could've had the same behavior using the word prototype (but sadly that wasn't a reserve word... I'll assume that's the reason why they went with class).
  • His view is that the fads at the time of its creation influenced JavaScript's addition of the new keyword to create the pseudo-classical inheritance mode, which should've never been there.
  • A problem with the new operator is that if you create a function that requires it but then you forget to type it in, you will pollute the global object instead of creating a new one. That's the reason why constructors are capitalized by convention, so people get used to expecting a new there.
  • Object.create, the method that provides the true prototypical inheritance, wouldn't be added until ECMAScript 5. So new precedes it by a decade, I believe.
  • But actually, even the concept of inheritance of any kind, he has become pretty skeptical of. His favorite aspect of JS has always been its functional behaviors, lexical scoping, closures, etc, and I think that's what's he advocates for object creation these days.

But, as I said, this was a very thorough explanation. Thanks!

Collapse
 
baboquivari profile image
Tony

"So when you try to access leo.prototype, leo doesn't have a prototype property so it will delegate that lookup to Animal.prototype which indeed does have a constructor property. If this paragraph didn't make sense, go back and read about Object.create above."

I think you meant 'leo.constructor' + 'constructor property'? :)

Collapse
 
venoel profile image
venoel

Perfect.

Collapse
 
alloverit88 profile image
alloverit88

Good article, thank you!
Very good presentation

Collapse
 
grepliz profile image
Liz Lam

This is an excellent post on explaining prototypes!! Thank you for sharing!

Collapse
 
shmdhussain12 profile image
Mohamed Hussain

One of the best article I have read about the JS object and how constructor and prototype chain works..thanks Tyler for this article.

Collapse
 
arnaudambro profile image
Arnaud Ambroselli

Great article, perfectly explained. I also learned a lot today ! Thanks !

Collapse
 
stevetwitte profile image
Stephen Taylor Witte

Awesome article! A great explanation of often misunderstood concepts in JavaScript.

Collapse
 
guergana profile image
Guergana Tzatchkova

Cool! Thanks for this post. I am subscribed to your newsletter. All the material is always very helpful.

Collapse
 
kensleyolewis profile image
klewis

Good article. Learned a lot today.

Collapse
 
gordan_cuic profile image
Gordan Cuic

Wow... great article, I've been struggling with this till now. Tnx a lot

Collapse
 
diegocrzt profile image
Diego Ramírez

Really Great, thank you Tyler