Comments on Prototypal Object-Oriented Programming using JavaScript

21 Reader Comments

Back to the Article
  1. JavaScript is indeed a versatile language. I’ve been able to implement responsive web design with a JS module I call jRWD. 12 lines of pure JavaScript replace countless @media queries. Details here: https://github.com/BlackMagic/jRWD

    Copy & paste the code below to embed this comment.
  2. Well this is one way to look at OOP but it is based on a fallacious premise.  JavaScript is not an OOP language and without major overhaul it will never be one. So please try not to use the OOP paradigm since it is very confusing to newbies and professionals alike.  The less we use improper terminology the better it will be.  JavaScript has some great features which are being obscured by all this messing around with trying to make it look like OOP.

    Copy & paste the code below to embed this comment.
  3. “Both have been researched and debated in the academic world since before I was born. Is one better than the other? There is no consensus on that.”

    There is no consensus??? It’s obvious that there is. The entire IT industry has spoken! It has roundly ignored prototypes, and for good reason. No other notable language created in the past quarter century has adopted object prototypes, except for 23-year-old Lua. The industry has shown a clear preference for class-based OOP, because while classes do have their problems, there is (currently) no better way to organize large-scale application code (say, in the hundreds of thousands of lines). Object prototypes would fall flat on its face here.

    Languages like Smalltalk and C++ are capable of writing programs as big as a million lines of code. JavaScript would be hopeless in these situations. Apparently, ECMA recognized this and tried to accommodate the popularity of classes.

    However, the latest ES6 class feature is only a half-assed implementation of class-based OOP. It’s just syntactic sugar over object prototypes. It still doesn’t support private state (member variables) in the way we expect. Member variables must be “faked” through a variety of workarounds such as programming convention, capturing normal variables with closures, and employing WeakMaps. ES6 is not a proper class-based OOP language like Smalltalk, C++, Java and Python.

    Let’s remember why JavaScript was created. Brendan Eich wanted a light, breezy, scripting language for the web browser that was flexible and forgiving. It was never intended for serious software engineering. The qualities that make it flexible (e.g, object prototypes and loose typing) are the same qualities that work against good engineering. For all of ECMA TC39’s efforts, this has not changed.

    Like I said, the IT industry has spoken. Nobody really wants object prototypes.

    JavaScript is not a misunderstood language. We know exactly what it is. JavaScript is a sloppy and undisciplined language wholly unsuitable for proper software engineering, esp. at scale.

    Copy & paste the code below to embed this comment.
  4. I’m not entirely convinced Javascript has serious purpose when it comes to software engineering. There are far more suitable languages that should be used. Not to say Javascript shouldn’t be used though. I guess it depends on the requirements of each individual case.

    Copy & paste the code below to embed this comment.
  5. Fairly novice here, but what’s with Snippet #12? Why would you use the arguments object when the function needs fixed arguments? Why do you use the Math object within when ‘uber’ is the Math object?

    Kind of threw me off the tutorial.

    Copy & paste the code below to embed this comment.
  6. I think that using Javascript is an individual case. Thanks for good exemples!

    Copy & paste the code below to embed this comment.
  7. Excellent article. Clearly understandable. I have some confusion with oop concept in javascript. but after reading this, I clearly understood. Thanks for sharing this article.

    Copy & paste the code below to embed this comment.
  8. I find the opening metaphor of prototype based vs classical oop inheritance very useful, thanks for the wrap up.

    Copy & paste the code below to embed this comment.
  9. No advertising,very good

    Copy & paste the code below to embed this comment.
  10. I think the biggest issue here is an understanding of how JS treats complex types (objects,arrays) vs simple types (string,number,bool). Most people expect a copy of every attribute when they create an object using ‘new’ however JS will only make copies of the simple data types and instead creates a pointer to the complex data types. If you included a property of this.name in Animal type, the two new Cats (puff and colonel) would have unique properties. However since the Array of offspring is just a pointer to the original, when you makeBaby is still is pointing to the original Animal.offspring. The same applies for passing values into a function. Pass an object as a parameter and any changes you make in that function to the object point to the original.

    Copy & paste the code below to embed this comment.
  11. It bugged me that you used gender, maybe use Kingdom instead, Animalia would be good…

    genericAnimal.kingdom = ‘Animalia’;

    Copy & paste the code below to embed this comment.
  12. Bit confused here…

    how this works ?

    this.events[‘click .expand’] = ‘expand’;

    Copy & paste the code below to embed this comment.
  13. An excellent article. Thank you for such a simple and informative explanation. This article actually made me wonder what it would be like to develop in JavaScript in “pure” prototype based OOP rather than using any type of class based system (including the imitation of class based OOP discussed in the article). So, I created a tiny library that removes any imitation of class based OOP from creating objects. It could also prove to be a useful learning tool when trying to understand/implement prototypal OOP without trying to wrap one’s mind around the class based imitation found in native JavaScript.

    I hope that it’s useful to someone, enjoy: https://www.npmjs.com/package/proto-proper

    Copy & paste the code below to embed this comment.
  14. Great article.

    JS is definitely a ...fun language =)

    Copy & paste the code below to embed this comment.
  15. advanced javascript like angularjs tutorial

    Copy & paste the code below to embed this comment.
  16. information about javascript code that is very helpful for me who is studying deeper .. keep working , thanks to quality article , if you need information about more try visiting..Sepatu Running Adidas

    Copy & paste the code below to embed this comment.
  17. Just out of curiousity, is this the right solution to the backbone.js problem? (add the following code before the new expand handler statement)

    if(!this.hasOwnProperty('events')) {
    this.events = Object.create(Object.getPrototypeOf(this).events)
    }

    Copy & paste the code below to embed this comment.
  18. Very good article and thank you for taking the time to write it.  I do have some differing opinions on whether or not the industry has decided on which version of OOP is best, but that is for another article.

    Copy & paste the code below to embed this comment.
  19. Since the prototype property is a reference, changing the prototype object’s properties at runtime will affect all objects using the prototype.

    Can you please explain this point?

    Only way I am able to modify the parent’s property is via child object’s _proto_ property. Still parent objects are without new property.

    Thanks a lot. Sorry for foolish question

    Copy & paste the code below to embed this comment.
  20. Sorry, commenting is closed on this article.