Last active
August 29, 2015 14:11
-
-
Save tcare/81452323f06a263ea1e9 to your computer and use it in GitHub Desktop.
ES6 Classes Syntax Example
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
class Parent { | |
constructor() { } // Custom constructor (optional.) If one is not provided, the example here is used a default constructor. | |
// Methods | |
method() { } // Simple instance methods | |
static staticMethod() { } // Static methods that can be called on the constructor, e.g. A.staticMethod. | |
// Getter/setter methods - similar to ES5 equivalents. | |
get prop() { return this.x; } | |
set prop(x) { this.x = x; } | |
}; | |
class Child extends Parent { // Classes usually extend other classes, but they can also extend other objects. | |
constructor(...args) { super(...args); } // Custom constructor (optional.) This example is the default constructor. | |
// Methods | |
method() { // Subclass methods can override their superclass method. | |
super.method(); // Explicit references using the 'super' keyword allow access to the parent methods. | |
super.prop = 1; // Properties on the parent class can be explicitly accessed using the 'super' keyword. | |
super['prop'] = 1; // As above. | |
} | |
static staticMethod() { // Static methods can also be overriden... | |
super.staticMethod(); // ...and can explicitly refer to their parent method. | |
} | |
get prop() { return super.x; } | |
set prop(x) { super.x = x; } | |
}; | |
// Creating and using instances | |
let parentInstance = new Parent(); | |
parentInstance.method(); | |
let childInstance = new Child(); | |
childInstance.method(); | |
// Using static methods | |
Parent.staticMethod(); | |
Child.staticMethod(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Shouldn't lines 25-26 be using
super.prop
?