Wednesday, December 14th, 2005

Javascript closures for object oriented Ajax

Category: Articles, JavaScript, Programming, Prototype

Alex Vollmer has posted a nifty article on using closures for object oriented ajax, using Prototype’s Ajax.Request object as the example. Many developers coming from the more static Java/C++/C# worlds have problems wrapping their head around closures when they first start doing ajax work with callbacks for XHR.

Alex’s writes:

…a closure allows JavaScript to use a dynamically-scoped variable which resolves the variable at runtime depending on the context in which it is invoked.

He goes on to illustrate how the use of “this” in javascript may trick up the java programmer:

Unlike Java where ‘this’ always refers to the instance containing the method, ‘this’ means different things in JavaScript….’this’ changes meaning depending on the context in which it is used

For much more detail on closures in JS, check out this long article on

Posted by Rob Sanheim at 1:35 pm
1 Comment

3.9 rating from 12 votes

1 Comment »

Comments feed

[…] Functions are always closures (combine w/ previous rule to create OOP) […]

Pingback by Ajaxian » JavaScript idioms you need to know — January 18, 2006

Leave a comment

You must be logged in to post a comment.