Created
March 14, 2017 09:11
-
-
Save aloerina01/d9537a60291363555b95df5c33a4f9da to your computer and use it in GitHub Desktop.
ES6 Proxy - 実用例ModuleBuilderでSingletonクラスをつくる
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
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; |
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
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