Last active
August 29, 2015 14:17
-
-
Save dandean/6248cba08cabd185bbce to your computer and use it in GitHub Desktop.
Holy shit private members!
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
var foo = Symbol('foo'); | |
class MyClass { | |
constructor() { | |
// Calling a private method!!! | |
this[foo](); | |
} | |
[foo]() { | |
alert('FOOOOOO!!!!!!'); | |
} | |
} | |
// Cannot call private method!!! Throws! | |
var myClass = new MyClass(); | |
myClass.foo(); |
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 BaseStore { | |
get(id) { | |
// return a single record... | |
} | |
[Symbol.for('store.insert')](record) { | |
// insert a record | |
} | |
} | |
class UsersStore extends BaseStore { | |
// Override BaseStore's private "insert" method using namespaced | |
// global symbols! | |
[Symbol.for('store.insert')](user) { | |
// Special logic when inserting user into store... | |
super[Symbol.for('store.insert')](user); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Now we can enforce the rule that consumers can only get data, and not manipulate data, on a store. Simultaneously we can provide basic functionality in a single base Store class, and extend/override for custom use cases.
This all works via babel right now.