Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prototypal vs Functional OOP in JavaScript

Tags:

javascript

oop

What are the pros and cons of each type/approach in writing Object Oriented scripts?

Personally, I have found closures (functional? approach) as a way to encapsulate state more natural and perhaps more elegant. I, however, have heard that this use of closures is slower in JavaScript implementations.

I would at least like to know where a prototypal approach would be most appropriate.

like image 322
fuentesjr Avatar asked Oct 12 '09 22:10

fuentesjr


People also ask

Is JavaScript OOP or functional?

What is JavaScript? JavaScript (often shortened to JS) is a lightweight, interpreted, object-oriented language with first-class functions, and is best known as the scripting language for Web pages, but it's used in many non-browser environments as well.

What is the difference between prototypal inheritance in JavaScript and classical object-oriented inheritance?

Classical inheritance is limited to classes inheriting from other classes. However prototypal inheritance includes not only prototypes inheriting from other prototypes but also objects inheriting from prototypes.

What is the main difference between prototypal and class inheritance?

The most important difference between class- and prototype-based inheritance is that a class defines a type which can be instantiated at runtime, whereas a prototype is itself an object instance.

What is prototypal inheritance in JavaScript?

The Prototypal Inheritance is a feature in javascript used to add methods and properties in objects. It is a method by which an object can inherit the properties and methods of another object. Traditionally, in order to get and set the [[Prototype]] of an object, we use Object. getPrototypeOf and Object.


2 Answers

"Functional" Style (most people would call this "traditional OOP"):

  • Advantage: OOP that works the way everyone (or at least Java programmers) is/are familiar with, including "truly" private methods and variables
  • Disadvantage: Javascript wasn't designed for this type of OOP, so you wind up jumping through a lot of hoops to make it work. These hoops make debugging more difficult, and also add a performance cost (although the exact cost will depend on how/how much you use them)

Prototype Style:

  • Advantage: It's OOP in the way Javascript was designed for.
  • Disadvantage: It's not what you're used to (unless you have a background in other protoype inheritance languages)

So if performance isn't a huge issue for you and you're only familiar with traditional OOP ... go for it (Pro Javascript Design Patterns, from APress, has a good pattern for this). But if performance matters or you're worried about the extra layer of abstraction complicating your debugging, take the time to read up on how prototype inheritance works; you'll be a better Javascript programmer for it.

P.S. If you're worried about not having true "private" methods with the prototype style, I strongly recommend reading: http://snook.ca/archives/javascript/no-love-for-module-pattern It provides a great explanation of why true "private" members are actually a bad thing (at least in most JS development environments).

like image 170
machineghost Avatar answered Sep 26 '22 10:09

machineghost


use prototypes to indicate object inheritance. it's pretty involved to duplicate that with functional code (I assume you mean the pattern where you have private members in a closure and return an object that contains your public members?)

function Animal() { }
function Cat () { }

Animal.prototype.sleep = function() { /* blah */ };
Cat.prototype = new Animal;
Cat.prototype.meow = function() { /* blah */ };

var simon = new Cat();
simon.sleep();
simon.meow();
like image 44
Jimmy Avatar answered Sep 25 '22 10:09

Jimmy