Skip to content

Instantly share code, notes, and snippets.

@gergelyke
Created January 19, 2018 21:57
Show Gist options
  • Save gergelyke/b23b37c3488cd64a1216433dd89754fa to your computer and use it in GitHub Desktop.
Save gergelyke/b23b37c3488cd64a1216433dd89754fa to your computer and use it in GitHub Desktop.
Private ES6 methods
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
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