Beni bu arastirmaya iten "CoffeeScript'teki -> ile => arasindaki farkin nedenir nedir?" sorusuydu. Sorunun cevabi yukaridaki bilginin varligindan haberi olan kisiler icin gayet net olarak dokumante edilmis CoffeeScript tarafindan. Durumu aciklamaya calisalim:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('#menu a').click(function(event) | |
{ | |
// $(this) | |
// Bize '#menu a'ya ait tiklanmis olan elementi gosterecektir. | |
$('#menu li').each(function(index) | |
{ | |
// $(this) | |
// Bize '#menu li'ye ait elementleri isaret edecektir. | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('#menu a').click(function(event) | |
{ | |
$me = $(this); | |
$('#menu li').each(function(index) | |
{ | |
// $(this) | |
// Bize '#menu li'ye ait elementleri isaret edecektir. | |
//$me | |
// Bize '#menu a'ya ait tiklanmis olan elementi gosterecektir. | |
}); | |
}); |
CoffeeScript ile calisiyorken nesne tabanli calismak zorunda hissediyorsunuz kendinizi, "class" etiketini gordukten sonra, ehehe. Elbette boyle bir durumda this kelimesi sizin icin gerekli oluyor, gerci bu ornekte sinif tanimlanmiyor ancak yine de ilintili. Lafi da fazla uzatmadan CoffeeScript'in kendi ornegi uzerinden olayi aciklamak istiyorum:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Account = (customer, cart) -> | |
@customer = customer | |
@cart = cart | |
$('.shopping_cart').bind 'click', (event) -> | |
# Burada @ bize bind'in icerisindeki 'this'e yonlendirecek, | |
# haliyle asagidaki degiskenler hata verecektir. Cunku | |
# bind'in icerisinde bunlar tanimli degil! | |
@customer.purchase @cart |
Sorun kendini bellli ettigine gore cozumune de asagidan ulasabilirsiniz:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Account = (customer, cart) -> | |
@customer = customer | |
@cart = cart | |
$('.shopping_cart').bind 'click', (event) => | |
# -> yerine => kullandiktan sonra CoffeeScript artik @ 'i | |
# kullandigimizda bind'e degil, sinifin kendisine atif yapacaktir. | |
# yani artik degiskenlerimiz sinifta tanimli oldguklarindan dolayi | |
# artik erisilebilir durumda olacaktirlar. | |
@customer.purchase @cart | |
# Peki ya burada bind'in kendisine atif yapmak icin ne gerekli? | |
# Uzuuun uzun 'this' yazilmasi gerekli. Biliyorum Coffee'nin | |
# kisa sozdizimine alistiktan sonra buyuk bir zulum bu ancak | |
# mecburiyet... :) |
Bu kadar... :)