2

I've been reading through JavaScript: The Good Parts and I'm currently on "Chapter 5: Inheritance."

From what I understand, using functional inheritance is preferred because it allows privacy for the properties and variables of an object while still allowing them to be called using methods outside of the object. However, it does seem like there is an advantage for prototypal inheritance because you can create a prototype object fairly easily (which makes understanding what the object is, to me, a little more concrete).

When should I choose one over the other? Should I always use functional inheritance whenever possible? Or are there "better" guidelines that I can follow to make that determination?

Dylan R
  • 213
  • 3
  • 11
  • I think they're slightly different, but it does seem like that question covers a lot of what I'm looking for (namely the issues with using functional vs protoypal inheritance). I was hoping there would be a quick and easy way to make the determination but apparently not. :) Thanks for the feedback. – Dylan R Apr 21 '13 at 19:45

1 Answers1

3

I've seen very little code that uses functional techniques as the primary form of inheritance. The vast majority I've seen uses prototypal inheritance. It's fast and easy to implement, and seems most natural to JavaScript.

That's not to say that functional inheritance should never be used, but I think you'll find prototypal more than sufficient for most applications.

Let's not also forget that you can still use some functional techniques within your prototypal inheritance, like giving each object it's own version of a function that closes over a variable in the constructor. So it doesn't need to entirely be one or the other.

Most important is to understand the concepts of prototypes and functions/closures. This alone will give you what you need to know in order to make appropriate decisions.

Pokey
  • 111
  • 1
  • 2
    You might be interested by [**this article**](http://www.richard-foy.fr/blog/2011/10/30/functional-inheritance-vs-prototypal-inheritance/) – plalx Apr 21 '13 at 22:23
  • @plalx That's a great article! It's especially relevant since I'm learning CoffeeScript, as well. – Dylan R Apr 21 '13 at 23:10