JS Classes

Creating classes by function and by class

by Trina Lu

Babel + JSX

const logger = (...message) => () => console.log(...message);

// by function
function ObjFunc() {
  this.privateMethod = logger('[ObjFunc] private method', this);
}

ObjFunc.staticMethod = logger('[ObjFunc] static method', this);
ObjFunc.prototype.publicMethod = logger('[ObjFunc] public method', this);

// by class
class ObjClass {
  constructor() {
    this.privateMethod = logger('[ObjClass] private method', this);
  }
  
  publicMethod() {
    logger('[ObjClass] public method', this)();
  }
  
  static staticMethod() {
    logger('[ObjClass] static method', this)();
  }
}

const objfunc = new ObjFunc();
const objclass = new ObjClass();