Skip to content

Instantly share code, notes, and snippets.

@tav
Created July 11, 2014 18:09
Show Gist options
  • Save tav/00e2d39e2e806e7d1b8b to your computer and use it in GitHub Desktop.
Save tav/00e2d39e2e806e7d1b8b to your computer and use it in GitHub Desktop.
> state = {user: 'tav', setUser: function(username) {
... console.log(this.user);
... this.user = username;
... }}
{ user: 'tav', setUser: [Function] }
> runCb = function(cb, value) {
..... cb(value);
..... }
[Function]
> runCb(state.setUser, 'seyi')
undefined
undefined
> keys = Object.keys(state)
[ 'user', 'setUser' ]
> for (var i = 0; i < keys.length; i++) {
..... key = keys[i];
..... value = state[key];
..... if (typeof value === 'function') {
....... state[key] = value.bind(state);
....... }
..... }
[Function]
> runCb(state.setUser, 'seyi')
tav
undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment