ricardomol
  • Ricardomol Notes
  • Frontend
    • Javascript Toolchain
    • Javascript
      • Quirks
      • Articles
        • Function caching
        • 12 JS Tricks
      • Closures
      • Assorted
      • ES6
      • this
      • OOP
      • Async Programming
      • Functional Programming
      • Typescript
    • React
      • Patterns
        • Render props
      • React Router
    • Webpack
    • CSS
      • Resources
  • Backend
    • Python
      • Shallow copy vs deep copy
      • Classes
      • Resources
      • Python C Extensions
      • Coroutines
      • Exceptions
      • Context managers
      • One-Liners
      • Open function
      • Object introspection
      • Targeting Python 2 + 3
      • For - else
      • Comprehensions
      • Lambdas
      • __slots__ magic
      • Collections
      • Enumerate
      • Mutation
      • Map, Filter and Reduce
      • Decorators
      • Sets
      • Fluent Python summary
      • Quizes / Tips
      • Generators
    • Django
      • Generic Relations
      • FBV's vs CBV's
      • ORM
      • DRF
    • RESTful Architecture
    • Resources
  • Databases
    • Joins
    • Normalization
    • PostgreSQL
  • DevOps
    • Docker
      • 0. Resources
      • 2. Services
      • 3. Swarms
      • 5. Stacks
      • 6. Deploy your app
    • CI
      • CI with Django
    • CD
    • PaaS
    • WSGI servers
    • Django
      • Django Deployment
    • Modern DevOps with Django
  • Git
    • Git
  • Comp Sci
    • Big O Notation
    • Patterns
    • Programming paradigms
  • Assorted
    • TCP vs UDP
    • Tests
    • MongoDB
    • Node
      • Resources
    • Go
    • HTTP vs HTTP2
    • GraphQL
    • Books
    • Vim
    • IPv4 vs IPv6
    • Regex
    • Redis
    • Celery
      • Brokers
    • Caching
  • SECURITY
    • Security
Powered by GitBook
On this page
  • Classical inheritance vs Prototypal inheritance?
  • Class Inheritance:
  • Prototypal Inheritance:
  • When is classical inheritance appropriate?
  • When is prototypal inheritance appropriate?
  • Composition vs Inheritance
  • "Favor object composition over class inheritance”
  • Why you might want to create static class members?
  1. Frontend
  2. Javascript

OOP

PreviousthisNextAsync Programming

Last updated 6 years ago

Classical inheritance vs Prototypal inheritance?

Class Inheritance:

Instances inherit from classes (like a blueprint — a description of the class)

Create sub-class relationships: hierarchical class taxonomies.

Instances are typically instantiated via constructor functions with the new keyword.

May or may not use the class keyword from ES6.

Prototypal Inheritance:

Instances inherit directly from other objects.

Instances are typically instantiated via factory functions or Object.create().

Instances may be composed from many different objects, allowing for easy selective inheritance.

When is classical inheritance appropriate?

Never, or almost never. Certainly never more than one level. Multi-level class hierarchies are an anti-pattern. I’ve been issuing this challenge for years, and the only answers I’ve ever heard fall into one of several . More frequently, the challenge is met with silence.

Creating is-a relationships.

When is prototypal inheritance appropriate?

There is more than one type of prototypal inheritance:

  • Delegation (i.e., the prototype chain).

  • Concatenative (i.e. mixins, `Object.assign()`)

  • Functional (Not to be confused with functional programming. A function used to create a closure for private state/encapsulation).

Each type of prototypal inheritance has its own set of use-cases, but all of them are equally useful in their ability to enable composition, which creates has-a or uses-a or can-do relationships as opposed to the is-a relationship created with class inheritance.

Composition vs Inheritance

"Favor object composition over class inheritance”

It means that code reuse should be achieved by assembling smaller units of functionality into new objects instead of inheriting from classes and creating object taxonomies.

In other words, use can-do, has-a, or uses-a relationships instead of is-a relationships.

Why you might want to create static class members?

Static class members (properties/methods) are not tied to a specific instance of a class and have the same value regardless of which instance is referring to it.

Static properties: typically configuration variables

Static methods: usually pure utility functions which do not depend on the state of the instance.

References

Quote from .

common misconceptions
“Design Patterns: Elements of Reusable Object-Oriented Software”
https://stackoverflow.com/questions/21155438/when-to-use-static-variables-methods-and-when-to-use-instance-variables-methods