Just posted another update on optimizing ads to app installs on Facebook.
Give it a read if you are interested.
We also launched an update on Balloonz: users can not create their own fillings to put in the balloons. Today, just five days after launch, we are crossing 3K users!
Have you thrown a balloon yet?
Tuesday, October 02, 2007
Wednesday, September 26, 2007
Throw a balloon at friends
The latest rage on Facebook is Balloonz!.
It's a fun little app that lets you stuff things in balloons and throw at friends. Friends can dodge them by sending them on to someone else. The fun continues until the balloon explodes...
Come, throw some! :)
It's a fun little app that lets you stuff things in balloons and throw at friends. Friends can dodge them by sending them on to someone else. The fun continues until the balloon explodes...
Come, throw some! :)
Recalling an interview on TV with Lata Mangeshkar
I was reading some coverage of the felicitations of the Indian cricket team for having won the world cup. Of course no one expected them to win. Every victory seemed like their last. But that didn't stop the ministers and the who's who to bask in refelcted glory.
The incident I was reminded of was what Lata Mangeshkar -- who if you don't know about happens to be the female voice of just about any Indian film song you heard from 1950 to 1980 -- recounted in a TV interview.
Lata had sung a "patriotic" song during the Indo-China war days in 1962 for India's prime minister Jawahar Lal Nehru. As it happened, Nehru was deeply touched by the song, and shed a tear. Lata was hurriedly called back to meet Nehru after the performance, and Nehru told her that she had made him cry. This incident became a huge rage. And it spun off a weird fad. Whenever Lata was performing live at a show, ministers and babus would sit in the front row, and have her perform the same song she did for Nehru. And... they would cry.
The incident I was reminded of was what Lata Mangeshkar -- who if you don't know about happens to be the female voice of just about any Indian film song you heard from 1950 to 1980 -- recounted in a TV interview.
Lata had sung a "patriotic" song during the Indo-China war days in 1962 for India's prime minister Jawahar Lal Nehru. As it happened, Nehru was deeply touched by the song, and shed a tear. Lata was hurriedly called back to meet Nehru after the performance, and Nehru told her that she had made him cry. This incident became a huge rage. And it spun off a weird fad. Whenever Lata was performing live at a show, ministers and babus would sit in the front row, and have her perform the same song she did for Nehru. And... they would cry.
Friday, September 21, 2007
This one's for the gallery
Six sixes off six balls. This one thing alone makes up for a lot of could-have-beens-that-werent from the Indian team. Yuvraj's the man!
Sunday, September 16, 2007
Tracking Facebook ad to install conversion
This is a problem we were looking into for some time, and we released an application to track these better for us.
Read more about it here.
It might be insightful anyway to read it if you want to advertise your apps on facebook.
Read more about it here.
It might be insightful anyway to read it if you want to advertise your apps on facebook.
Friday, September 07, 2007
Gripe of the day
Why do sites require me to provide my birthday, my zip code, my email, my dog's name, my school's name, my roommate's name, my favorite color... Why? When will websites learn? Why is this information relevant to them?
I just today tried to sign up for Mashable. I don't even remember why. Here's what their sign up page looks like:

I tried ot leave a few of them blank and sign up. It didn't work. No I did not sign up thank you. My stupid old internet life is fine enough without sharing all my personal data with every tom, dick and harry.
I just today tried to sign up for Mashable. I don't even remember why. Here's what their sign up page looks like:

I tried ot leave a few of them blank and sign up. It didn't work. No I did not sign up thank you. My stupid old internet life is fine enough without sharing all my personal data with every tom, dick and harry.
Have you shared a memory yet?
Where have you been? Here's a link to the Facebook app. Shared Memories.
Vote for it on best of facebook here.
Also -- if you are building applications for Facebook, you might be interested in the blog post here.
Vote for it on best of facebook here.
Also -- if you are building applications for Facebook, you might be interested in the blog post here.
Looking forward to Shoot em up
I've seen the trailer online a few times and I am all for it. Shoot 'em up, Clive!
Ever since I saw him in Greenfingers, I've been a fan of Clive Owen. That was years ago, and this guy seems to go from strenght to stength. Shoot em up seems to have everything going for it -- they even roped in Monica Bellucci! Paul Giamatti is an odd guy -- Sideways seemed ot suit him just right -- but he looks irritating enough in the promos that him getting his ass kicked is probably going to be double the fun.
Trailer...
Ever since I saw him in Greenfingers, I've been a fan of Clive Owen. That was years ago, and this guy seems to go from strenght to stength. Shoot em up seems to have everything going for it -- they even roped in Monica Bellucci! Paul Giamatti is an odd guy -- Sideways seemed ot suit him just right -- but he looks irritating enough in the promos that him getting his ass kicked is probably going to be double the fun.
Trailer...
Tuesday, August 21, 2007
Friday, August 17, 2007
Jambool's alexa graphs now online...
Posted on Jambool blog... here.
Aside from that, we happened to get the Shyam Benegal movie Suraj ka Satwan ghoda from Netflix this week. I had seen this a long time ago -- and it came out much before the multi-threaded stories were all the rage. This is pre-Tarantino, pre-Amorres Perros. It makes a great watch, and definitely one of the smarter movies to come out from India. It's a pity it is not as well known.
I've been collecting Anthony Lane movie reviews on Jambool here -- they make an absolutely fabulous read every time. What's your favorite Anthony Lane review?
Aside from that, we happened to get the Shyam Benegal movie Suraj ka Satwan ghoda from Netflix this week. I had seen this a long time ago -- and it came out much before the multi-threaded stories were all the rage. This is pre-Tarantino, pre-Amorres Perros. It makes a great watch, and definitely one of the smarter movies to come out from India. It's a pity it is not as well known.
I've been collecting Anthony Lane movie reviews on Jambool here -- they make an absolutely fabulous read every time. What's your favorite Anthony Lane review?
Tuesday, August 14, 2007
Update..
It has been a gruelling time, trying to get Jambool to the next stage. The best advice I ever got for my startup was to find a co-founder. And Reza's the best. Those who knew us at Amazon probably remember our April fool emails and announcements.
The best part of working with someone you enjoy working with is that however hard your job is, in the end it's heck of a lot of fun.
Jambool's Alexa rank is on the rise. It's moved to within the top 150K for the last two weeks' averages. 3 months is still low, but we'll get there!
I saw Bourne Ultimatum -- and I think the second one was better, though people claim this one tops that. What the movie did interestingly was to quietly spoof the deeds of the current administration -- from wiretapping and refusing to answer to reason and so on -- but did so without making it too farcical.
I saw another movie called "Once" -- an Irish movie with good soul and music (pun unintended). Recommended when you can find it. It's playing in a few art house theaters in San Francisco these days.
P.S. I just happened to press "Ctrl-E" when editing this blog. And it worked. Blogger editor support Emacs keys? Or maybe it is Safari? Something feels better in this world already. The geek in me awakens. :-)
The best part of working with someone you enjoy working with is that however hard your job is, in the end it's heck of a lot of fun.
Jambool's Alexa rank is on the rise. It's moved to within the top 150K for the last two weeks' averages. 3 months is still low, but we'll get there!
I saw Bourne Ultimatum -- and I think the second one was better, though people claim this one tops that. What the movie did interestingly was to quietly spoof the deeds of the current administration -- from wiretapping and refusing to answer to reason and so on -- but did so without making it too farcical.
I saw another movie called "Once" -- an Irish movie with good soul and music (pun unintended). Recommended when you can find it. It's playing in a few art house theaters in San Francisco these days.
P.S. I just happened to press "Ctrl-E" when editing this blog. And it worked. Blogger editor support Emacs keys? Or maybe it is Safari? Something feels better in this world already. The geek in me awakens. :-)
Friday, August 03, 2007
Amazon 'Flexible' Payments launches...
Amazon.com launched Flexible Payments web service this week.
It's already on Reddit -- with plenty of people welcoming it. Like people say over there, Amazon's track record of extremely easy to use web services API should definitely give Google and Paypal worries with this launch.
Why am I blogging about it? And is it really that cool?
Well -- the primary reason I am blogging about it is that this was some of the best things I was involved with at Amazon. Over a period of about a year and a half that I was involved with Amazon FPS, we conceived the product and built a truly solid team in India to develop this product.
And yes, it is really that cool. The Payment Instructions component of the API give you, the developer, unparalleled control over the transactions that follow. Using these instructions you can describe a whole negotiation between multiple parties. Each participant in the transaction can have their own set of instructions -- these are rules that have to match against each other in order for the transaction to go through. Can you imagine the possibilities? You can choose who not to do business with, what payment methods to accept (only accept direct debit, get the fees down), set minimum amounts, etc., etc.
Among all the services Amazon's rolled out, this one is probably the most complex in its interface to the developers. The world's been raving about and using S3 (and so does Jambool). Amazon FPS is the other service that I am sure is going to get lots of positive buzz. And deservedly so, even if it is a somewhat biased view.
Kudos to a lot of people and dear friends at Amazon who've brought FPS to light.
It's already on Reddit -- with plenty of people welcoming it. Like people say over there, Amazon's track record of extremely easy to use web services API should definitely give Google and Paypal worries with this launch.
Why am I blogging about it? And is it really that cool?
Well -- the primary reason I am blogging about it is that this was some of the best things I was involved with at Amazon. Over a period of about a year and a half that I was involved with Amazon FPS, we conceived the product and built a truly solid team in India to develop this product.
And yes, it is really that cool. The Payment Instructions component of the API give you, the developer, unparalleled control over the transactions that follow. Using these instructions you can describe a whole negotiation between multiple parties. Each participant in the transaction can have their own set of instructions -- these are rules that have to match against each other in order for the transaction to go through. Can you imagine the possibilities? You can choose who not to do business with, what payment methods to accept (only accept direct debit, get the fees down), set minimum amounts, etc., etc.
Among all the services Amazon's rolled out, this one is probably the most complex in its interface to the developers. The world's been raving about and using S3 (and so does Jambool). Amazon FPS is the other service that I am sure is going to get lots of positive buzz. And deservedly so, even if it is a somewhat biased view.
Kudos to a lot of people and dear friends at Amazon who've brought FPS to light.
Friday, June 29, 2007
In India in July
I will be in India in July. While mostly a family and friends visit, Jambool will of course still be high on my priorities.
People whom I haven't already touched base with: I will be in Delhi for a week till about the 11th, and I will be in Bangalore for a day on the 7th and the morning of the 8th. Most of the rest of the time I will be in the beautiful city of Chandigarh.
People whom I haven't already touched base with: I will be in Delhi for a week till about the 11th, and I will be in Bangalore for a day on the 7th and the morning of the 8th. Most of the rest of the time I will be in the beautiful city of Chandigarh.
More updates on Jambool...
In case you haven't read them yet, we released an update to the Firefox browser extension, and also rolled out some changes to the clipping dialogs. More here.
Sunday, June 24, 2007
What does Google know about me?
Awfully lot, I imagine.
Let's see -- I started on Google search. I jumped on Gmail -- the first time I created an account. I used Google news -- though I hated their personalized news stuff. And since I was always logged on in Gmail, I couldn't really turn off this personalization. Then Google bought Blogger -- the site that hosts this blog -- and went on to buy yet more stuff (You tube for one) where I may have logged in. Everywhere I turned soon, Google login was following me. And then they bought doubleclick. Now, add the two together, that covers probably an incredible amount of information about me being tracked by them over time.
In general, I've loved Google products -- just like most of the people I know. Even then, it is a little unsettling to know that people with the smartest data mining algorithms are going to have a lot of data about me to play around with.
So, what's the solution? For one, I've blocked Google.com cookies. I use Apple's iChat if I need to use Google talk (so I don't have to have Gmail in the browser). I use a POP client to get my Gmail. This also, unfortunately means I can't login to Blogger and Adsense on the same machine -- or at least that browser. So I sometimes fire up Safari if I do have the urge to write a blog, and shut it down once I am done.
On a different, unrelated note, Jambool's releasing updates to its site. If you have an account, do come back and check it out!
Let's see -- I started on Google search. I jumped on Gmail -- the first time I created an account. I used Google news -- though I hated their personalized news stuff. And since I was always logged on in Gmail, I couldn't really turn off this personalization. Then Google bought Blogger -- the site that hosts this blog -- and went on to buy yet more stuff (You tube for one) where I may have logged in. Everywhere I turned soon, Google login was following me. And then they bought doubleclick. Now, add the two together, that covers probably an incredible amount of information about me being tracked by them over time.
In general, I've loved Google products -- just like most of the people I know. Even then, it is a little unsettling to know that people with the smartest data mining algorithms are going to have a lot of data about me to play around with.
So, what's the solution? For one, I've blocked Google.com cookies. I use Apple's iChat if I need to use Google talk (so I don't have to have Gmail in the browser). I use a POP client to get my Gmail. This also, unfortunately means I can't login to Blogger and Adsense on the same machine -- or at least that browser. So I sometimes fire up Safari if I do have the urge to write a blog, and shut it down once I am done.
On a different, unrelated note, Jambool's releasing updates to its site. If you have an account, do come back and check it out!
Saturday, June 16, 2007
Ocean's thirteen -- a joke I felt 'in' with...
All successful movies tend to become franchises of sort these days. Those involved milk it repeatedly for what it's worth. Whether it's the Pirates, the Shrek or the Ocean's teens. Back in India, movie makers are picking up on it as well -- Dhoom, Don, Koi Mil Gaya, Hera Pheri have all had or are going to have sequels.
Ocean's eleven was entertaining. Ocean's twelve kept true to its entertainment value. With Ocean's thirteen, the movie makers seem to have got rid of any pretense they may have had about making a movie. It just seems like a bunch of friends having a good time, and expecting you'll just follow along. Every other line is some sort of an inside joke that you feel left out of.
Except there was this one time, where someone walks up to Pacino in the party, and congratulates him on "the enormity of his success!" Aha! (See previous post) I think I was the only one who laughed at this one. Oh well.
Anyway -- I haven't posted for a while, and this was a good excuse to break the hiatus. Jambool is keeping me busy... but now that our beta is live, I hope to get time to do some other things once in a while too. :)
Ocean's eleven was entertaining. Ocean's twelve kept true to its entertainment value. With Ocean's thirteen, the movie makers seem to have got rid of any pretense they may have had about making a movie. It just seems like a bunch of friends having a good time, and expecting you'll just follow along. Every other line is some sort of an inside joke that you feel left out of.
Except there was this one time, where someone walks up to Pacino in the party, and congratulates him on "the enormity of his success!" Aha! (See previous post) I think I was the only one who laughed at this one. Oh well.
Anyway -- I haven't posted for a while, and this was a good excuse to break the hiatus. Jambool is keeping me busy... but now that our beta is live, I hope to get time to do some other things once in a while too. :)
Thursday, April 19, 2007
The enormousness of some mistakes
The Economist style guide is making the rounds on the Internet. It's a decent read, and I am posting it here to point more people to it.
Also, I wanted to rant about the very frequent misuse of the word "enormity" that I see everywhere. I've seen this on otherwise excellent literary stuff, and it seems to be very widespread. First off, "enormity" is not the same as "large" or "enormous" -- in fact it means something of an enormous mistake, an extreme absurdness. When news sites report about the "physical enormity" of players, and "enormity" of devastation, it reads as an enormity in itself.
"Enormousness" is the word you might be after if you've used enormity wrongly. It is, unfortunately, a larger word, not so easy on the tongue or the ears, and therefore suffers from lack of love.
Also, I wanted to rant about the very frequent misuse of the word "enormity" that I see everywhere. I've seen this on otherwise excellent literary stuff, and it seems to be very widespread. First off, "enormity" is not the same as "large" or "enormous" -- in fact it means something of an enormous mistake, an extreme absurdness. When news sites report about the "physical enormity" of players, and "enormity" of devastation, it reads as an enormity in itself.
"Enormousness" is the word you might be after if you've used enormity wrongly. It is, unfortunately, a larger word, not so easy on the tongue or the ears, and therefore suffers from lack of love.
Tuesday, April 17, 2007
On this and that..
There's a lot going on right now -- Jambool is getting ready for the next major release. So there's little time for anything else for me right now.
That said, I couldn't stay away from "Grindhouse" and the "The Host." Grindhouse is by Tarantino and Rodriguez, and it's everything you'd want from them. Rodriguez delivers it much better than Tarantino though, I think. If zombies and gore and yuck is your thing, this movie delivers. It's a send up on umpteen B-movies from the 70s, and Rodriguez executes on the idea simply brilliantly. For me, it was a laugh riot.
"The Host" is new movie by the director of "Barking dogs never bite." The dogs movie has been one of the best movies from South Korea, and "The Host" is an excellent follow up. It's a film that doesn't try to fit into any genre, and embraces humor and irony where you'd least expect.
Jumping topics... I upgraded to Firefox 2.0 recently on my Mac, and it is ridiculously slower than 1.5. So much so that I've often switched to using Safari or the older install of Firefox.
That said, I couldn't stay away from "Grindhouse" and the "The Host." Grindhouse is by Tarantino and Rodriguez, and it's everything you'd want from them. Rodriguez delivers it much better than Tarantino though, I think. If zombies and gore and yuck is your thing, this movie delivers. It's a send up on umpteen B-movies from the 70s, and Rodriguez executes on the idea simply brilliantly. For me, it was a laugh riot.
"The Host" is new movie by the director of "Barking dogs never bite." The dogs movie has been one of the best movies from South Korea, and "The Host" is an excellent follow up. It's a film that doesn't try to fit into any genre, and embraces humor and irony where you'd least expect.
Jumping topics... I upgraded to Firefox 2.0 recently on my Mac, and it is ridiculously slower than 1.5. So much so that I've often switched to using Safari or the older install of Firefox.
Friday, March 23, 2007
Indians (not) at the world cup...
Just when we were thinking this Indian side has the best shot at the cup, they go out and prove a point or two -- other teams are just better than them.
Wednesday, March 21, 2007
Closures in Javascript
I've grown up on C, and then a bit more on C++. I used to write a lot of networking and systems code, and then application level code to build backend distributed systems for an online retailer. Having dabbled a little bit in Java, and even less in Lisp, I've adopted Ruby as the language I mostly code in these days. But all these languages were a charm to get used to and develop in when I compare them with Javascript.
Why? Several reasons, but primarily because so much of the Javascript is about the browser, and while how you do something depends entirely on which broiwser your code is going to run on, there is hardly any good development and testing environment. To top that, I've not had the pleasure of reading a really good reference-like book for Javascript. If you know one, do tell me about it.
With the lack of books, one tends to rely on Google search to throw up some useful results. But some things are what you have to go through to pick up anyway. That brings me to closures. Though people have written about closures, they tend to only cover one aspect of lexical binding, and I wanted to add to this pool of collective online intelligence.
Closures let you define functions on the fly that are bound to the local lexical scope. Using these closures you can execute code that will use and affect variable references as available in the lexical scope of where the function is defined. Here's an example:
Run it here
.
The function f1 above returns a closure. The function returned is bound to the lexical scope of function f1. Once f1 is done, one would think that the all local variables etc are removed, and calling the function that is returned back by f1 should really use "B" as the value of the letter. Or maybe you would expect it to raise an error, because the scope of function f2 doesn't exist anymore. But if you run it, you'll still find that the function f returned by f1() call alerts with "A."
Once you see this, it is somewhat obvious what is happening. Except one thing, and we'll come to that shortly. What seems to be happening is that when the function in f2 is created (or rather the closure), it keeps track of its lexical scope. So all the references available in its scope are available to it. The reference that is returned is a reference to a set of code and its scope as was available then. That scope is what is used to execute the code in the closure.
What seems to be slightly less obvious is that the closure captures the references and not the values of these references in its scope. I.e., the closure does not copy all the values of the variables in its scope and keep them around for later. It keeps track of the references in its scope. If their values change by the time the closure is called, then the result would be different. Here is an update to the above example.
If you run the code above (here), you'll find that the call from f2 uses the value "A" for letter and the call from f3 uses the value "C."
The reason is again obvious once you see it, but it is easy to trip over this when using closures bound to variables in a loop. In the code above, f2 creates a new variable letter in its scope. This variable is not available in the lexical scope of the closure returned by f1. f1 uses the earlier defined letter. However, f3 does not define a new variable -- it updates the variable in global scope, the same variable that is also used by f1. The call from f3 to the closure evaluates the variable (the reference) 'letter' and it evaluates to "C" and not "A."
An important ramification of this is that when you are creating closures as callbacks inside a loop, and binding them to the variables that change with every iteration of the loop, you have to be extra careful. Unless you want to use these variables' values as they exist after the loop is done (or whenever the callback is called -- which might make it unpredictable), you have to create unique lexical references for each of these variables. A simple way to do this is to write a method that returns back the actual closure bound to its local variables. Every time this function is called, it generates a new scope for the closure. An example:
If you run the code above (here), you will see three alerts with values 0, 1 and 2, followed by three alerts with value 3.
The reason, as you would have gathered by now, is that in the testGoodFunctor, each closure gets a unique reference for the alert call. However, the testBadFunctor provides the same reference "i" to the closure. When the closure is actually called, the value of i is the value it is supposed to have at the end of the loop.
There now, that's closures for you. And yes, there is one more thing to remember. Be careful not to use "this" to point to the object inside the closure (unless you know exactly what you are doing). "this" is not a locally defined reference, and when you closure is invoked, "this" may point to something completely different. If you want to invoke your methods on a specific object, it is better to create methods that are bound to the specific object. For example, like this:
Here, run it.
Update: Fixed the links to the running javascript samples.
Why? Several reasons, but primarily because so much of the Javascript is about the browser, and while how you do something depends entirely on which broiwser your code is going to run on, there is hardly any good development and testing environment. To top that, I've not had the pleasure of reading a really good reference-like book for Javascript. If you know one, do tell me about it.
With the lack of books, one tends to rely on Google search to throw up some useful results. But some things are what you have to go through to pick up anyway. That brings me to closures. Though people have written about closures, they tend to only cover one aspect of lexical binding, and I wanted to add to this pool of collective online intelligence.
Closures let you define functions on the fly that are bound to the local lexical scope. Using these closures you can execute code that will use and affect variable references as available in the lexical scope of where the function is defined. Here's an example:
function f1() {
var letter = "A";
return function() { alert(letter); }
}
function f2() {
var letter = "B";
var f = f1();
f();
}
f2();
Run it here
.
The function f1 above returns a closure. The function returned is bound to the lexical scope of function f1. Once f1 is done, one would think that the all local variables etc are removed, and calling the function that is returned back by f1 should really use "B" as the value of the letter. Or maybe you would expect it to raise an error, because the scope of function f2 doesn't exist anymore. But if you run it, you'll still find that the function f returned by f1() call alerts with "A."
Once you see this, it is somewhat obvious what is happening. Except one thing, and we'll come to that shortly. What seems to be happening is that when the function in f2 is created (or rather the closure), it keeps track of its lexical scope. So all the references available in its scope are available to it. The reference that is returned is a reference to a set of code and its scope as was available then. That scope is what is used to execute the code in the closure.
What seems to be slightly less obvious is that the closure captures the references and not the values of these references in its scope. I.e., the closure does not copy all the values of the variables in its scope and keep them around for later. It keeps track of the references in its scope. If their values change by the time the closure is called, then the result would be different. Here is an update to the above example.
var letter = "A";
function f1() {
return function(callfrom) { alert( callfrom + " " + letter); }
}
function f2() {
var letter = "B";
var f = f1( );
f("call from f2");
}
function f3() {
letter = "C";
var f = f1( );
f( "call from f3" );
}
f2();
f3();
If you run the code above (here), you'll find that the call from f2 uses the value "A" for letter and the call from f3 uses the value "C."
The reason is again obvious once you see it, but it is easy to trip over this when using closures bound to variables in a loop. In the code above, f2 creates a new variable letter in its scope. This variable is not available in the lexical scope of the closure returned by f1. f1 uses the earlier defined letter. However, f3 does not define a new variable -- it updates the variable in global scope, the same variable that is also used by f1. The call from f3 to the closure evaluates the variable (the reference) 'letter' and it evaluates to "C" and not "A."
An important ramification of this is that when you are creating closures as callbacks inside a loop, and binding them to the variables that change with every iteration of the loop, you have to be extra careful. Unless you want to use these variables' values as they exist after the loop is done (or whenever the callback is called -- which might make it unpredictable), you have to create unique lexical references for each of these variables. A simple way to do this is to write a method that returns back the actual closure bound to its local variables. Every time this function is called, it generates a new scope for the closure. An example:
function myFunctor( x) {
return function() { alert( x); }
}
function testGoodFunctor() {
var functors = new Array;
for( var i = 0; i < 3; ++i ) {
functors.push( myFunctor(i) );
}
for( var j = 0; j < functors.length; ++j ) {
functors[j]();
}
}
function testBadFunctor() {
var functors = new Array;
for( var i = 0; i > 3; ++i ) {
functors.push( function() { alert(i); } );
}
for( var j = 0; j < functors.length; ++j ) {
functors[j]();
}
}
testGoodFunctor();
testBadFunctor();
If you run the code above (here), you will see three alerts with values 0, 1 and 2, followed by three alerts with value 3.
The reason, as you would have gathered by now, is that in the testGoodFunctor, each closure gets a unique reference for the alert call. However, the testBadFunctor provides the same reference "i" to the closure. When the closure is actually called, the value of i is the value it is supposed to have at the end of the loop.
There now, that's closures for you. And yes, there is one more thing to remember. Be careful not to use "this" to point to the object inside the closure (unless you know exactly what you are doing). "this" is not a locally defined reference, and when you closure is invoked, "this" may point to something completely different. If you want to invoke your methods on a specific object, it is better to create methods that are bound to the specific object. For example, like this:
function getObjectBoundMethod( obj, f ) {
return function() { return f.apply(obj,arguments); };
}
function f() {
this.g = function(x) { alert( "x = " + x ); };
var closure = getObjectBoundMethod( this, this.g );
return closure;
}
var to_call = f();
to_call(5);
Here, run it.
Update: Fixed the links to the running javascript samples.
Subscribe to:
Posts (Atom)