Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set binding of methods in an object #9

Open
jcblw opened this issue Jan 20, 2015 · 3 comments
Open

set binding of methods in an object #9

jcblw opened this issue Jan 20, 2015 · 3 comments

Comments

@jcblw
Copy link
Contributor

jcblw commented Jan 20, 2015

I think it might be cool to be able to pass an object to bound. Then that object's methods would be bound to itself. Its pretty useful to know this will alway be in context of the object in which the method is created I can see it being useful in a situation like this.

function Foo(){
    this.baz = 'qux';
    bound( this ); // binds all methods in the object to the object.
}

Foo.prototype.bar = function(){
  console.log( this.baz );
}

Foo.prototype.qux = function(){
    setTimeout( this.bar, 1000 ); // this.bar will console log 'qux';
}
@andyburke
Copy link
Contributor

Perhaps a slight change to the API:

function Foo(){
    this.baz = 'qux';
    bound( this, '__' ); // binds all methods in the object to the object.
}

Foo.prototype.__bar = function(){
  console.log( this.baz );
}

Foo.prototype.qux = function(){
    setTimeout( this.bar, 1000 ); // this.bar will console log 'qux';
}

That is:

bound( <obj>, <prefix> )

would bind all methods on <obj> that start with <prefix> as the method name with the <prefix> stripped.

@jcblw
Copy link
Contributor Author

jcblw commented Jan 20, 2015

would this be an requirement or an option?

@andyburke
Copy link
Contributor

Maybe we should only take an object to make things clearer:

bound( {
    target: this,
    prefix: '__'
} );

bound( {
    target: this
} );

bound( {
    target: emit,
    events: {
        'foo.bar': 'onBar',
        'foo.baz': 'onBaz'
    },
    obj: this
} );

It's a bit more verbose, but very explicit.

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

No branches or pull requests

2 participants