Skip to content

Instantly share code, notes, and snippets.

@dandean
Last active August 29, 2015 14:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dandean/6248cba08cabd185bbce to your computer and use it in GitHub Desktop.
Save dandean/6248cba08cabd185bbce to your computer and use it in GitHub Desktop.
Holy shit private members!
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();
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);
}
}
@dandean
Copy link
Author

dandean commented Mar 14, 2015

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment