Jure Cuhalev

12 Oct, 2007

Double bind() for better `this` experience

Posted by: Jure Cuhalev In: Planet Kiberpipa| Planet LiveCD| Tech| javascript

I am doing some developing with prototype lately and I was having some issues with context of `this` variable. The problem is that javascript is different from python in a way that defines local variable scope of class methods.

So here is short snippet with double bind(this) that allows one to reference class `this`. In essence, you just have to make sure that every call is wrapped into bind(this) if you want to be able to access `this` of class instance.


// class code goes around and params dict
new Ajax.Request('json-url',
{ parameters: params,
method: 'post',
onSuccess: function(transport) {
response = transport.responseText.evalJSON();
response.images.map(function(i) {
this.local_func(i.image.prop);
}.bind(this));
}.bind(this)
});

Related posts:

  1. TextMate tip: Making single apostrophes double
  2. MSN Live Search python API access
  3. Posting to Koornk using Python
  4. Enable your Linux CUPS printers in Leopard
  5. Cinelerra dapper packages are in

No Responses to "Double bind() for better `this` experience"

Comments are closed.

Flickr PhotoStream

    Workspacebooks I am readingQWIKI TODO

About

jure100px_colour Jure Cuhalev is an Open Source Hacker, with background in Social Sciences and Usability and User Experience (UX).