Created
May 22, 2017 22:03
-
-
Save wanderer/7c3bae49e7902eb6e9f3ca301000b612 to your computer and use it in GitHub Desktop.
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 ProviderUpdater { | |
constructor (provider) { | |
this._provider = provider | |
this._handler = { | |
get: (obj, name) => { | |
return this._provider[name] | |
}, | |
set: (obj, name, value) => { | |
this._provider[name] = value | |
} | |
} | |
this._proxy = new Proxy(this._provider, this._handler) | |
} | |
get provider () { | |
return this._proxy | |
} | |
set provider (provider) { | |
this._provider = provider | |
} | |
} | |
const providerA = {} | |
const updater = new ProviderUpdater(providerA) | |
const provider = updater.provider | |
provider.a = 55 | |
console.log(provider.a) | |
// update the provider | |
const providerB = {} | |
updater.provider = providerB | |
// a no longer exists | |
console.log(provider.a) | |
provider.b = 99 | |
console.log(provider.b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment