bradxr
8/4/2018 - 3:18 PM

Use class Syntax to Define a Constructor Function

Constructors can be defined using class syntax, eliminating the need to define a constructor function class syntax is only syntax, not a class-based implementation of object oriented paradigm

// STANDARD

// defining a constructor function
var SpaceShuttle = function(targetPlanet) {
  this.targetPlanet = targetPlanet;
};

// using new keyword to instantiate an object
var zeus = new spaceShuttle('Jupiter');


// ES6

// defining a constructor function with the class keyword
class SpaceShuttle {
  constructor(targetPlanet) {
    this.targetPlanet = targetPlanet;
  }
};

// using new keyword to instantiate an object
const odin = new SpaceShuttle('Mars');