{"id":84,"date":"2007-10-12T01:46:18","date_gmt":"2007-10-12T00:46:18","guid":{"rendered":"http:\/\/www.kiberpipa.org\/~gandalf\/blog\/?p=84"},"modified":"2007-10-12T01:46:18","modified_gmt":"2007-10-12T00:46:18","slug":"double-bind-for-better-this-experience","status":"publish","type":"post","link":"https:\/\/www.jurecuhalev.com\/blog\/double-bind-for-better-this-experience\/","title":{"rendered":"Double bind() for better `this` experience"},"content":{"rendered":"<p>I am doing some developing with <a href=\"http:\/\/www.prototypejs.org\/\">prototype<\/a> 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.<\/p>\n<p>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.<\/p>\n<p><code><br \/>\n\/\/ class code goes around and params dict<br \/>\n    new Ajax.Request('json-url',<br \/>\n                    { parameters: params,<br \/>\n                      method: 'post',<br \/>\n                      onSuccess: function(transport) {<br \/>\n                        response = transport.responseText.evalJSON();<br \/>\n                        response.images.map(function(i) {<br \/>\n                          this.local_func(i.image.prop);<br \/>\n                        }.bind(this));<br \/>\n                      }.bind(this)<br \/>\n                      });<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[18,14],"tags":[],"class_list":["post-84","post","type-post","status-publish","format-standard","hentry","category-javascript","category-tech"],"acf":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/posts\/84","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/comments?post=84"}],"version-history":[{"count":0,"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/posts\/84\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/media?parent=84"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/categories?post=84"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/tags?post=84"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}