Skip to content

Instantly share code, notes, and snippets.

@aloerina01
Created March 14, 2017 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aloerina01/d9537a60291363555b95df5c33a4f9da to your computer and use it in GitHub Desktop.
Save aloerina01/d9537a60291363555b95df5c33a4f9da to your computer and use it in GitHub Desktop.
ES6 Proxy - 実用例ModuleBuilderでSingletonクラスをつくる
import ModuleBuilder, { Handlers } from './ModuleBuilder';
/**
* Module
*/
class Config {
constructor() {
this._fetchConfig((result) => {
this._accountId = result.accountId;
this._name = result.name;
this._age = result.age;
});
}
/**
* @return {Object} instance
*/
getInstance() {
return instance;
}
/**
* @return {Number}
*/
age() {
return this._age;
}
_fetchConfig(callback) {
// 略
}
// 以下略
}
const instance = new ModuleBuilder().class(Config).handler(Handlers.Singleton).build();
export default instance;
import Config from './Config';
const config = Config.getInstance();
if (config.age() > 18) {
// 省略
}
// ちょっと微妙なのが、getInstanceしなくても使えちゃうこと…
console.log(Config.age()); // 17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment