Skip to content

Instantly share code, notes, and snippets.

@rkyoku
Created October 4, 2019 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rkyoku/774f6a4563f0841e787a299066f79e6f to your computer and use it in GitHub Desktop.
Save rkyoku/774f6a4563f0841e787a299066f79e6f to your computer and use it in GitHub Desktop.
Classes in ES6
/**
* Standard class
*/
class Car {
// Public properties are not widely supported yet
// https://caniuse.com/#feat=mdn-javascript_classes_public_class_fields
// Our constructor
constructor(brand, model) {
// Define two public properties inside the ctor
this.brand = brand;
this.model = model;
}
// This is a getter (i.e. mycar.brandAndModel, and not mycar.brandAndModel())
// This pseudo-property cannot be set, only read
get brandAndModel() {
return `${this.brand} ${this.model}`;
}
// This is a classic method
getBrandAndModel() {
return this.brandAndModel;
}
}
/**
* Abstract class
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment