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.
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!
"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'? :)
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.
Funny enough but true
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
classandnew:classto JS is actually a step backwards. In my view, it is a misnomer because you could've had the same behavior using the wordprototype(but sadly that wasn't a reserve word... I'll assume that's the reason why they went with class).newkeyword to create the pseudo-classical inheritance mode, which should've never been there.newoperator 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 anewthere.Object.create, the method that provides the true prototypical inheritance, wouldn't be added until ECMAScript 5. Sonewprecedes it by a decade, I believe.But, as I said, this was a very thorough explanation. Thanks!
"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'? :)
This is an excellent post on explaining prototypes!! Thank you for sharing!
Good article, thank you!
Very good presentation
One of the best article I have read about the JS object and how constructor and prototype chain works..thanks Tyler for this article.
Perfect.
Wow... great article, I've been struggling with this till now. Tnx a lot
Cool! Thanks for this post. I am subscribed to your newsletter. All the material is always very helpful.
Great article, perfectly explained. I also learned a lot today ! Thanks !
Awesome article! A great explanation of often misunderstood concepts in JavaScript.
Good article. Learned a lot today.
Really Great, thank you Tyler