[CoffeeScript] 1. Variable and function.

//Variable 
message = "Ready for some Coffee?"
alert(message)

/*
//in Javascript
var message = "Ready for some Coffee?";
alert(message);*/

/**
    We can see that in CoffeeScript:
    1. No var
    2. No ';'
*/

/*
//Function in Javascript
function coffee(){
    return confrim("Ready for some Coffee?");
}

var coffee = function(){
    return confrim("Ready for some Coffee?");
coffee();
}*/

//1 tab or 2 spaces indented 
//-> converts to function(){
//Always has a return value
coffee = ->
    answer = confrim "Ready for some Coffee?"
    //"your answer is "+answer
    "your answer is #{answer}"

//add param to the function    
coffee = (message) ->
  answer = confirm message
  "Your answer is #{answer}"
  
 /**
    call function
 */ 
 //function                     call
coffee = ->                    coffee()
coffee=(message) ->            coffee("Yo"), coffee "Yo"
coffee=(message, other) ->     coffee("Yo", 2), coffee "Yo", 2


/**
    If we want a default message
*/
coffee = (message="Ready for some Coffee?") ->
  answer=confirm message
  "Your answer is #{answer}"
 //"Ready for some Coffee?" is the default value

 

posted @ 2014-08-14 03:41  Zhentiw  阅读(180)  评论(0编辑  收藏  举报