Saturday, September 3, 2011

Some interesting usages of closure in Javascript

Closures are one of the most powerful features of ECMAScript (Javascript), but they can not be exploited without properly understanding them. Some very interesting usage of closures for circumventing few problems are listed below.
  1. Problem: Add click listeners to a array of DOM elements, clicking on element should alert index of element in array.
Wrong solution :


for(var i=0;i<elArray.length;i++){
elArray[i].on('click',function(){alert(i)});
}


The problem here is that clicking on any node alerts the size of array instead of its index. Reason being, value of 'i' is equal to elArray.length on termination of loop and ever after.


Right Solution
inducing closure is the key here... we can use anonymous function to induce the scope needed to create the multiple closure


for(var i=0;i<elArray.length;i++){
elArray.on('click',(function(){
return function(){
alert(i);
};
})())
}


this simply works, but might not be very intuitive to do. There are other ways of achieving the same but I like this best. Would love to hear about benefits of other approaches that you might figure out.

Tuesday, September 2, 2008

Google chrome

Tonight google launched a new browser named chrome . so its about 2 hour,and now I am writing this blog on chrome :) .
Google Chrome has a simple user interface with a sophisticated core to enable the modern web . At it’s core is Webkit, an open-source rendering enginer - the same used by Safari. The browser in Android also uses Webkit. The browser also uses a multi-process architecture. Each tab within the browser runs independently, making the browser faster and more responsive. If one tab slows down or crashes, it doesn’t affect the others.

With the launch of Chrome a obvious issue arises , Google has been a prime Firefox promoter and distributer, and the source of its (comparative) wealth, so Google's focus on Chrome will inevitably diminish its support for Firefox. Whether Chrome will hurt Firefox more than it hurts IE or Safari remains to be seen.
However chrome is announced to be a opensource project , and also it does not force you to work with other google services ,as Larry page says "We don’t want to live in a world where things can’t be improved and that’s why Google is committed to the open-source platform with Chrome. "

you can download Chrome here



Wednesday, May 7, 2008

Iphone in India...

This is a really good news for gizmo freaks in India,Vodafone is set to offer the apple iphone to its subscribers in India before 2008-end.Vodafone refused to divulge any further details regarding the date of launch,the model or the price however a company official promised that the prices will be "competitive".
Iphone's inaugural price was Rs 15,960 for 8GB and Rs 19,960 for 16GB with a two-year service agreement.

Wednesday, April 30, 2008

IBM Racetrack Memory

IBM Racetrack is a new breakthrough in physics research that will lead to faster, cheaper and higher capacity data storage devices.

RaceTrack, unlike conventional memory which relies on electronic charges to store data, uses the spin of an electron. IBM says the new technology breakthrough may help increase the amount of storage in devices such as iPods by 100-fold and pave the way for entirely new products.


By: Sara Smith
Apr 11, 2008, 3:20 PM EDT

International Business Machines Corp., the world's largest company-owned laboratory, has announced new technology that stems from an area of physics called spintronics. The new breakthrough is called Racetrack which can delivery faster, cheaper and higher capacity data storage.

RaceTrack, unlike conventional memory which relies on electronic charges to store data, uses the spin of an electron.

The new technology breakthrough may help increase the amount of storage in devices such as iPods by 100-fold and pave the way for entirely new products.

A media player could use Racetrack to hold as many as 500,000 songs or about 3,500 movies. However, Researchers IBM point out that the new memory is still 7 to 8 years away.

"We have demonstrated the physics and materials underlying racetrack memory," said Dr Stuart Parkin, an IBM fellow at the Almaden laboratory.

The researchers say that it will take another four years to build the prototype and then three to four more years to refine the prototype for commercial use.

The memory is called race track because of the method use to store data in boundaries between magnetic regions in nanowires.