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
Life Insurance Calgary
ReplyDeleteSuper Visa Canada
best performing mutual funds
Mortgage Broker Calgary
Life Insurance Vancouver
Exempt Market Dealers
Edmonton Mortgage Broker
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
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
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
niche job masker wajah alami
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/
2018112 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
ReplyDeletelove 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
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
here 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
Obat 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 .
ReplyDeletenow present in your city
ReplyDelete
ReplyDeleteشركة تنظيف شقق بالرياض
شركة تنظيف واجهات زجاج بالرياض
شركة تنظيف موكيت بالرياض
شركة عزل خزانات بالرياض
instagram takipçi satın al
ReplyDeleteinstagram takipçi satın al
takipçi satın al
takipçi satın al
instagram takipçi satın al
takipçi satın al
instagram takipçi satın al
aşk kitapları
tiktok takipçi satın al
instagram beğeni satın al
youtube abone satın al
twitter takipçi satın al
tiktok beğeni satın al
tiktok izlenme satın al
twitter takipçi satın al
tiktok takipçi satın al
youtube abone satın al
tiktok beğeni satın al
instagram beğeni satın al
trend topic satın al
trend topic satın al
youtube abone satın al
beğeni satın al
tiktok izlenme satın al
sms onay
youtube izlenme satın al
tiktok beğeni satın al
sms onay
sms onay
perde modelleri
instagram takipçi satın al
takipçi satın al
tiktok jeton hilesi
pubg uc satın al
sultanbet
marsbahis
betboo
betboo
betboo
It’s genuinely very complicated in this active life to listen news on Television, thus I simply use world wide web for that purpose, and take the most recent news. I am reading this wonderful article to increase my know-how. ufa168
ReplyDeleteIs there anyone who can recommend the best product among the ones listed here?
ReplyDeleteBest Alarm Clock
This comment has been removed by the author.
ReplyDeleteThanks you so much ....It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. We provide best realtime project centers in chennai we offer project for final year students
ReplyDeleteThank you so much for the information, Best regards from Website
ReplyDelete