var i = 0; el.onclick = function() { print(i); }; i++;
In the above example what will be printed? This person argued that, intuitively, "0" should be printed. Of course the correct answer is that "1" will be printed. I can understand how this may be confusing to someone new to Javascript. However, take another example:
var i = 0; var func = function() { i++; }; func(); print(i);
If I were to show this piece of code to someone, they would probably say that it makes sense. Of couse "1" will be printed. It is intuitive (and correct). Following that line of reasoning:
var i = 0; var func = function() { print(i); }; i++; func();
This code also seems to make sense, especially in light of the previous example. The only real difference is that this example reads/prints the variable, and the previous one modified the variable. In both cases "1" will be printed.
Now we're back to the original code:
var i = 0; el.onclick = function() { print(i); }; i++;
Note that the only difference between this piece of code and the previous one is when the function is executed. In one the function is executed right away, in the other the function is executed as a click handler. In both cases i++ occurs first and in both cases "1" is printed. This is consistent behavior and somewhat shows how closures can morph from something which "just makes sense" into something that isn't quite as intuitive.
The reason for this behavior is that closures work by pointing to the original variable by reference, if you will, not by value - even for primitives. So when the anonymous function runs, i is still pointing back to the original i which has been incremented to 1.
Thanks for writing this up, James. This is a great explanation of something that beginners often trip over.
ReplyDeleteHi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from Javascript Online Training from India . or learn thru JavaScript Online Training from India. Nowadays JavaScript has tons of job opportunities on various vertical industry. ES6 Training in Chennai
DeleteGreat Article
DeleteIEEE Final Year Projects for CSE
IEEE Project Centers in Chennai
patriots jerseys, http://www.newenglandpatriotsjerseys.us/
ReplyDeletesaints jerseys, http://www.neworleanssaintsjerseys.us/
fitflop, http://www.fitflop.in.net/
karen millen uk, http://www.karenmillendressesoutlets.co.uk/
air jordan 11 free shipping, http://www.airjordan11.net/
hollister canada, http://www.hollistercanada.com/
hollister uk, http://www.hollistershirts.co.uk/
tods outlet, http://www.todsoutlet.us.com/
air jordan 4 free shipping, http://www.airjordan4.org/
pandora jewelry, http://www.pandora.eu.com/
coach outlet online, http://www.coachoutletstores.com.co/
tiffany and co, http://www.tiffanyandco.in.net/
prada outlet, http://www.pradaoutlet.us/
ravens jerseys, http://www.baltimoreravensjerseys.us/
supra shoes, http://www.suprashoes.us.com/
hogan,hogan outlet,scarpe hogan,hogan sito ufficiale,hogan interactive
michael kors outlet, http://michaelkors.outletonlinestores.us.com/
juicy couture tracksuit, http://www.juicycoutureoutlet.net/
chicago bulls, http://www.chicagobullsjerseys.net/
jets jersey, http://www.newyorkjetsjersey.us/
cai2015912
Life Insurance Calgary
ReplyDeleteSuper Visa Canada
best performing mutual funds
Mortgage Broker Calgary
Life Insurance Vancouver
Exempt Market Dealers
Edmonton Mortgage Broker
tods
ReplyDeleteair jordan 12
prada sunglasses
burberry sale
michael kors bags
burberry outlet
nike discount
clarks outlet
fitflops
ferragamo sale
vans shoes
north face coat
coach bags sale
michael kors outlet online sale
coach handbags
nike high heels online
hermes belt
joe's new balance
columbia outlet store
air jordan 14
canada goose cheap
salvatore ferragamo outlet
christian louboutin shoes
cheap rolex watches
reebok outlet
north face coats for women
oakley sunglasses
jordan 11
celine bag
salomon boots
cheap gucci shoes
ralph lauren clearance
jordan shoes
coach purses outlet
michael kors handbags clearance outlet
cheap canada goose coats
polo factory store
jordan 6
coach factory outlet
wholesale nike
kids jordans
hermes bags
abercrombie outlet
ghd flat iron
1013wjl
longchamp handbags
ReplyDeletebabyliss flat iron
michael kors wallet sale
ugg outlet online
san antonio spurs
moncler outlet store
new york giants
nike mercurial
nike air max uk
prada outlet online
rolex watches,rolex watches,swiss watches,watches for men,watches for women,omega watches,replica watches,rolex watches for sale,rolex replica,rolex watch,cartier watches,rolex submariner,fake rolex,rolex replica watches,replica rolex
oakley sunglasses wholesale
ugg clearance
michael kors outlet
mbt shoes outlet
thomas sabo outlet
michael kors outlet
ysl outlet
woolrich outlet store
replica handbags,replica handbags outlet,cheap handbags,purses and handbags,wholesale replica handbags,replica bags,bags outlet,replica handbags wholesale,cheap purses,discount handbags,handbags sale,wholesale purses,handbags and purses,replica purses
1111minko
chenlina20160319
ReplyDeletenike store
canada goose outlet store
ghd hair straighteners
louis vuitton bags
canada goose jackets
pandora outlet
michael kors outlet
kate spade handbags
oakley sunglasses
louis vuitton outlet
louis vuitton outlet
canada goose jackets
michael kors outlet
asics shoes
nike running shoes for women
tory burch shoes
nhl jerseys
coach outlet
coach outlet store
coach factorty outlet
kate spade outlet
coach factory outlet
oakley outlet
michael kors outlet
kobe 8
true religion jeans outlet
coach outlet store online
copy watches
kobe 10
michael kors handbags
kobe 11
louis vuitton handbags
timberland outlet
cheap jordan shoes
nike huarache shoes
louis vuitton handbags
michael kors outlet
coach factory outlet
replica watches
coach outlet
as
lululemon uk
ReplyDeleteferragamo shoes
michael kors outlet
links of london jewellery
futbol baratas
tiffany jewelry
burberry outlet online
ralph lauren outlet
louis vuitton neverfull
mulberry sale
omega watches
ralph lauren outlet
longchamp outlet online
ray ban sunglasses
prada outlet
oakley sunglasses wholesale
tory burch outlet
nike tn pas cher
asics
cheap oakley sunglasses
hermes outlet
louis vuitton handbags
tiffany jewellery
police sunglasses
ray-ban sunglasses
michael kors factory outlet
christian louboutin shoes
fitflops clearance
coach outlet
michael kors sale
louis vuitton sunglasses for women
michael kors uk outlet
polo ralph lauren
ray-ban sunglasses
coach outlet
chanyuan0607
Michael Kors Outlet Coach Outlet Red Bottoms Kevin Durant Shoes New Balance Outlet Adidas Outlet Coach Outlet Online Stephen Curry Jersey Vans Outlet Ralph Lauren Outlet
ReplyDeleteUgg Boots Sale UGGS For Women Skechers Go Walk Adidas Yeezy Boost Adidas Yeezy Adidas NMD Coach Outlet North Face Outlet Ralph Lauren Outlet Puma SneakersPolo Outlet
Under Armour Outlet Under Armour Hoodies Herve Leger MCM Belt Nike Air Max Louboutin Heels Jordan Retro 11 Converse Outlet Nike Roshe Run UGGS Outlet North Face Outlet
Adidas Originals Ray Ban Lebron James Shoes Sac Longchamp Air Max Pas Cher Chaussures Louboutin Keds Shoes Asics Shoes Coach Outlet Salomon Shoes True Religion Outlet
jianbin0811
ReplyDeletemoncler jackets
michael kors outlet
cheap oakley sunglasses
canada goose outlet store
ralph lauren femme
the north face jackets
the north face outlet
tory burch outlet
camisetas futbol baratas
ugg outlet
rolex watches
ralph lauren uk
michael kors outlet clearance
ugg outlet
iphone case uk
tiffany outlet
adidas uk
basketball shoes,basketball sneakers,lebron james shoes,sports shoes,kobe bryant shoes,kobe sneakers,nike basketball shoes,running shoes,mens sport shoes,nike shoes
coach outlet
louis vuitton bags
air jordan 13
mizuno running shoes
gucci outlet
michael kors factory outlet
fred perry polo
cartier outlet
cheap jordan shoes
louis vuitton pas cher
ugg clearance
ugg boots clearance
ugg outlet
louis vuitton handbags outlet
nike outlet
uggs outlet
north face jacket
nike store uk
jianbin0811
ReplyDeletecartier sunglasses for men
true religion jeans
celine outlet online
polo ralph lauren
lululemon pants
michael kors outlet
oakley sunglasses uk
ugg boots sale
true religion outlet
nike roshe run
michael kors handbags
uggs outlet
football shirts
louis vuitton outlet
ugg outlet
versace sunglasses
calvin klein underwear
cheap oakley sunglasses
chicago blackhawks
michael kors outlet
lacoste shirts
michael kors handbags clearance
coach outlet
juicy couture tracksuit
swarovski outlet
nike air max
uggs outlet
michael kors canada
uggs outlet
versace sunglasses on sale
rolex watches outlet
kate spade handbags
adidas nmd
christian louboutin outlet
prada outlet
michael kors outlet online
ysl outlet online
JIANBIN0811
ReplyDeletebeats headphones
ray-ban sunglasses
cheap oakley sunglasses
louis vuitton outlet
toms outlet
ugg boots uk
longchamp handbags
longchamp outlet
coach outlet online
sac louis vuitton pas cher
ugg boots clearance
ugg boots clearance
jordan 13
abercrombie outlet
air max uk
chrome hearts eyewear
lebron james shoes
fitflops sale
rolex watches
ugg outlet store
timberland shoes
fitflops shoes
polo ralph lauren outlet
supra shoes sale
lacoste pas cher
kate spade uk
ugg uk,ugg outlet,ugg boots outlet
fitflops shoes
michael kors bags
coach outlet online
toms outlet
Ralph Lauren Outlet
ReplyDeleteUgg Boots Sale UGGS For Women Skechers Go Walk Adidas Yeezy Boost Adidas Yeezy Adidas NMD Coach Outlet North Face Outlet Ralph Lauren Outlet Puma SneakersPolo Outlet
Under Armour Outlet Under Armour Hoodies Herve Leger MCM Belt Nike Air Max Louboutin Heels Jordan Retro 11 Converse Outlet Nike Roshe Run UGGS Outlet North Face Outlet
Adidas Originals Ray Ban Lebron James Shoes Sac Longchamp Air Max Pas Cher Chaussures Louboutin Keds Shoes Asics Shoes Coach Outlet Salomon Shoes True Religion Outlet
New Balance Outlet Skechers Outlet Nike Outlet Adidas Outlet Red Bottom Shoes New Jordans Air Max 90 Coach Factory Outlet
This blog is so nice to me. I will continue to come here again and again. Visit my link as well. Good luck
ReplyDeletecara menggugurkan kandungan
niche job masker wajah alami
ReplyDeleteCoach Outlet ED Hardy Outlet Coach Outlet Store Online Kate Spade Outlet Cheap Jordans Coach Purses Coach Outlet Kate Spade Outlet Toms Outlet Louis Vuitton
ReplyDeletemichael kors outlet
ReplyDeleteoakley sunglasses outlet
air max 95
toms shoes outlet
air jordan retro
columbia outlet online
michael kors outlet
giuseppe zanotti sneakers
oakley sunglasses
chaussures louboutin
mt0619
تمتع باقوي العروض الهائلة ومميزة من شركة رش مبيدات بالدمام والتي تعمل علي خدمات رش الحشرات وطرق المكافحة وابادتها تماما.
ReplyDeletehttp://abraj-dubai.net/%D8%B4%D8%B1%D9%83%D8%A9-%D8%B1%D8%B4-%D9%85%D8%A8%D9%8A%D8%AF%D8%A7%D8%AA-%D8%A8%D8%A7%D9%84%D8%AF%D9%85%D8%A7%D9%85-%D8%A8%D8%A7%D9%84%D8%A7%D8%AD%D8%B3%D8%A7%D8%A1/
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout obat pembesar penis
ReplyDeletenmd adidas
ReplyDeletepandora jewelry
ralph lauren outlet
moncler coats
mbt shoes
michael kors handbags
fred perry polo
hermes handbags
adidas yeezy boost
coach factorty outlet
Coach Outlet Store Online Kate Spade Outlet Cheap Jordans Coach Purses Coach Outlet Kate Spade Outlet Nike Air Max Shoes Red Bottom Shoes Adidas Yeezy Boost 350 Adidas Original
ReplyDelete2018112 leilei3915
ReplyDeletemichael kors outlet
michael kors outlet
ray ban sunglasse
oakley sunglasses wholesale
coach outlet online
ugg boots
polo ralph lauren
pandora jewelry
moncler coats
coach outlet online
good obat telat datang bulan
ReplyDeletenmd
ReplyDeletemichael kors handbags
http://www.kobeshoes.uk
nike hyperdunk 2017
adidas tubular
golden goose sneakers
nike flyknit
goyard handbags
adidas tubular shadow
birkin bag
kate spade outlet
ReplyDeletembt shoes outlet
pandora charms sale clearance
michael kors outlet clearance
nba jersey
pandora jewelry
ultra boosts
coach factory outlet online
coach outlet store online
christian louboutin outlet
shenyuhang20180424
love shayari in english
ReplyDeleteHopefully today enjoyable, thanks for sharing information
ReplyDeleteObat TBC Sampai Sembuh
Obat Hipertensi Secara Efektif
Obat Susah Buang Air Besar
Pengobatan Demam Berdarah Baik Untuk Semua Usia
Pengobatan Rematik Sampai Sembuh
Obat Atasi Kulit Bersisik Dan Gatal
love shayari
ReplyDeletelove shayari in hindi
love shayari in english
There are times that you can even get affirmed inside minutes and get the cash you require inside hours. You can in a split second pay your bills in a day or two. Auto title credits give you the cash you require when you require it! car title loans chicago
ReplyDeletelove shayari in english
ReplyDeleteشركة رش مبيدات بجازان
ReplyDeletegreat article Obat Kuat
ReplyDeleteHopefully today enjoyable, thanks for sharing information
ReplyDeleteObat TBC Sampai Sembuh
Obat Jantung Koroner Secara Alami
Obat Hipertensi Secara Efektif
Pengobatan Demam Berdarah Baik Untuk Semua Usia
Pengobatan Rematik Sampai Tuntas
Obat Atasi Kulit Bersisik Dan Gatal
Jordans 11
ReplyDeletePandora
Kyrie Irving Shoes
Jordans 11
Nike Air Max 270
Pandora Jewelry
Air Jordan 9
Air Jordan 4 Retro
Yeezy boost 350 v2
Pandora Jewelry Official Site
Nicole20180904
Yeezy boost 350 v2
ReplyDeleteAir Jordan 11
Jordan 11
Kyrie Irving Shoes
Jordan Retro
Pandora Jewelry Official Site
Air Max 270
Pandora Jewelry Official Site
Jordan 4 Retro
Red Bottom Shoes For Women
Lindsey20180913
Interesting and nice information, thanks for sharing :)
ReplyDeletePengangkatan Kista Bartholin Tanpa Operasi
Obat Penurun Berat Badan Alami
Pengobatan Keloid Secara Efektif
Pengobatan Luka Gangren Secara Optimal
Cara Mengobati Pembengkakan Prostat
Cara Ampuh Atasi Fatty Liver
Terima kasih artikelnya sangat membantu sekali mudah-mudahan artikel ini bermanfaat bagi semua.
ReplyDeleteCara Mengatasi Penyakit Kuning Yang Tak Kunjung Sembuh
Obat Herbal Untuk Penyakit Chikungunya
Obat Flu Dan Batuk Anak
Obat Herbal Untuk Sakit Perut Melilit
Obat Untuk Mengatasi Pudarnya Warna Kulit
Solusi Paling Tepat Untuk Penyakit Kanker Ovarium
GBWhatsApp APK
ReplyDeleteWhatsApp Plus apk
ReplyDeletehere we want to cover this great article, and we also share the article to all of you .
ReplyDeleteciri viagra asli
manfaat viagra
tips bercinta saat hamil
tips bercinta dengan suami
tips agar tahan lama saat bercinta
nice
ReplyDeletecara menggugurkan hamil
cara menggugurkan kehamilan
My first online casino solely best casino in which I played. Just want to highlight a very beautiful design and convenience and beauty in all games. The presence of public tables testifies first of all to casino honesty - nobody twists the roulette wheel specially - at least =).
ReplyDeleteObat Viagra merupakan obat kuat yang paling populer di dunia . Maka dari itu tak heran jika ada oknum yang memanfaat untuk membuat produk tiruan . Viagra sangat membantu sekali bagi kaum pria , karena manfaat viagra dapat menambah rasa percaya diri saat melakukan hubungan .
ReplyDeletejordan 13
ReplyDeletenike sneakers for men
adidas tubular
nike air max 90
michael kors
hermes handbags
mlb jerseys
adidas yeezy boost
air max 95
timberland boots
now present in your city
ReplyDelete