SORU
8 Mart 2011, Salı


Varsayılan sıralama fonksiyonu parametre

this, ben okurken işlev bağımsız değişkenleri için varsayılan değerler: bit geldi

fill = (container, liquid = "coffee") ->
  "Filling the #{container} with #{liquid}..."

Çok hoş, ama sonra bunu denedim:

fill = (container="mug", liquid = "coffee") ->
  "Filling the #{container} with #{liquid}..."

alert fill(liquid="juice")

ve "Filling the juice with coffee..." ile beklenmedik bir uyarı aldı. O zaman bu denedim:

fill = (container="mug", liquid = "coffee") ->
  "Filling the #{container} with #{liquid}..."

alert fill(null, "juice")

ve işe yaradı. Güzel değil. Daha iyi bir yolu yoktur, ya da bu deyimsel bu?

CEVAP
9 Mart 2011, ÇARŞAMBA


fill = ({container, liquid} = {}) ->
     container ?= "mug"
     liquid ?= "coffee"

     "Filling the #{container} with #{liquid}..."

alert fill(liquid: "juice", container: "glass")
alert fill()
fill = (quantity="500 mL", {container, liquid} = {}) ->
     container ?= "mug"
     liquid ?= "coffee"

     "Filling the #{container} with #{quantity} of #{liquid}..."

alert fill("1L", liquid: "juice", container: "glass")
alert fill()
alert fill "1L"
alert fill "1L", liquid: "water"

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Kai Moosmann

    Kai Moosmann

    5 Temmuz 2006
  • NextKsa

    NextKsa

    7 EKİM 2009
  • UberFacts

    UberFacts

    26 EKİM 2013