Skip to content

Instantly share code, notes, and snippets.

@richrdkng
Last active August 4, 2016 11:29
Show Gist options
  • Save richrdkng/4d81512887058cb035db to your computer and use it in GitHub Desktop.
Save richrdkng/4d81512887058cb035db to your computer and use it in GitHub Desktop.
ES6 self - Using "self" in ES6 classes in a consistent way. An important and useful feature similar to PHP's self:: current class accessor.
/*
|----------------------------------------------------------------------------------------------------------------------
| ES6 self template
|
| Similar to PHP's self:: current class accessor
|----------------------------------------------------------------------------------------------------------------------
*/
const self = class Class {
constructor() {}
};
export default self;
// or in NodeJS
module.exports = self;
/*
|----------------------------------------------------------------------------------------------------------------------
| Example
|----------------------------------------------------------------------------------------------------------------------
*/
const self = class Color {
static fromString(string = "0") {
return parseInt(string);
}
constructor() {
this._rgb = 0x000000;
}
parseString(string) {
// Using self to invoke a static function rather than
// call Color.fromString(), just use self.fromString();
this._rgb = self.fromString(string);
}
};
export default self;
// or in NodeJS
module.exports = self;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment