Skip to content

Instantly share code, notes, and snippets.

@DenisIzmaylov
Last active November 6, 2016 12:08
Show Gist options
  • Save DenisIzmaylov/e4d56b3dddcec243e08b to your computer and use it in GitHub Desktop.
Save DenisIzmaylov/e4d56b3dddcec243e08b to your computer and use it in GitHub Desktop.
Key difference between OOP and FP in JavaScript

Key difference between OOP and FP in JavaScript

In OOP we have mixed instance data and class functions:

class Animal { 
  constructor(name) {
    this.name = name;
  }
  speak() {
    console.log(this.name + ' makes a noise.');
  }
}
const instance = new Animal('Johny');
instance.speak();

In Functional Programming we have instance data and functions seperated:

const createAnimal = (name) => (this_) => {
  this_.name = name;
};
const speak = () => (this_) => {
  console.log(this_.name + ' makes a noise.');
};
const instance = {};
createAnimal('Johny')(instance);
speak()(instance);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment