Classes vs Functions
in JavaScript

Encapsulation

class Hey {
  constructor (something) {
    this.is = something
  }
  get something () {return this.is}
  set something (clever) {this.is = clever}
  regular (method) {return this.is + method}
  static something () {return 'amazing'}
}
var hey = new Hey('nasty')
console.log(hey.something)
// nasty
hey.something = 'really'
console.log(hey.something)
// really
console.log(hey.regular('method'))
// reallymethod
console.log(Hey.something())
// amazing
var Hey = (something) => {
  var self = {
    something,
    regular: (method) => self.something + method
  }
  return self
}

Hey.something = () => 'amazing'

Inheritance

Classes vs Functions in JavaScript

By Simeon Velichkov

Classes vs Functions in JavaScript

  • 496