Jure Cuhalev

12 Oct, 2007

Double bind() for better `this` experience

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

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. Usability reflections: How Google failed to keep their BlackBerry experience simple
  4. Black / Dark Google for Earth Hour
  5. Marko Samastur – Easy deployment of site-extensions with a browser plugin [firefox3 launch party notes]

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

Comments are closed.