Last active
August 4, 2016 11:29
-
-
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.
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
/* | |
|---------------------------------------------------------------------------------------------------------------------- | |
| 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