Created
October 4, 2019 11:09
-
-
Save rkyoku/774f6a4563f0841e787a299066f79e6f to your computer and use it in GitHub Desktop.
Classes in ES6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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