Created
January 19, 2018 21:57
-
-
Save gergelyke/b23b37c3488cd64a1216433dd89754fa to your computer and use it in GitHub Desktop.
Private ES6 methods
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
const transformName = Symbol('private:transformName'); | |
class Apple { | |
constructor () { | |
console.log('i am created') | |
} | |
[transformName] (name) { | |
return name.split('').reverse().join(''); | |
} | |
fallOn (person) { | |
console.log(`Fell on ${this[transformName](person)}`) | |
} | |
} | |
module.exports = Apple |
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
const Apple = require('./apple'); | |
// this outputs "i am created" | |
const apple = new Apple(); | |
// this outputs "Fell on Newton" | |
apple.fallOn('notweN') | |
// this will be a runtime error | |
apple[Symbol('private:transformName')]() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment