Coffeescript
----> Install node. js
----> Installation Coffeescript
Statement:
Note: There is no semicolon, the statement ends with a new line; multiple statements written to the same row require semicolons to denote the end of a statement (not commonly used);
Continuation to the next line by adding \ Representation statements at the end of a row;
Variable:
Unlike JS, coffeescript variables do not need to be defined, and all variables are local variables by default.
Declaring global variables: global.myglobalvariable= "I am calamus!"
Replace with Window object in browser: window.myglobalvariable= "I am calamus!"
Comments:
Single-line comment (does not exist after compilation): Begins with #, and the content after the # in the middle of the line is also the comment content.
#注释内容;
Multiline comments (rarely used, compiled to JS after/**/): # # #开头和结尾的注释块
###
Comment Block Contents
###
function Call :
The priority of the function call: The parameter is assigned to the function from inside out, that is, the recent principle.
Console.log ("Hello,calamus!" "Hello,calamus!" // the parentheses can be omitted if there are parameters, but sometimes a practical parenthesis is required to disambiguate Math.pow 2,3// call function without arguments, must be practical parentheses date.now ()
control Structure : Not practical curly braces, using indentation control code execution
If , else, and else if statements :
Calamus=16; if calamus>5 "Calamus is greater than 5" if calamus>15 " Calamus is greater than 5 "
Single-line form:
if true= =true
unless statement (the branch statement block is executed when the test condition is an equivalent false value):
Day= "Monday"unless day[0]== "S" "Today is a weekday!" //The compiled JS code is: var day;day= "Monday"; if (day[0]!== "S") { "today is a weekday!" }
Switch statement:
// compile to JS will add break Switch things " Ice " white" "Grass" then console.log "green" // can also be useful then shorten the statement to one line else "Gray"
comparison operators :
Coffeescript and JavaScript comparison operator conversions
Coffeescript |
Javascript |
is,== |
=== |
Isnt |
!== |
Not |
! |
and |
&& |
Or |
|| |
True,yes,on |
True |
False,no,off |
False |
@,this |
This |
Of |
Inch |
Inch |
|
Array:
Languages =["中文版", "Chinese", "French"=[ "English", "Chinese", "French "] // not practical comma can also languages =[ " English " " Chinese "" French "]
Sequence:
NUM=[0..9] // compile to num=[0,1,2,3,4,5,6,7,8,9]; NUM=[0...9] // compile to num=[0,1,2,3,4,5,6,7,8]; three points with no trailing boundaries
Simple objects:
Author={name: "Calamus", age:21} // can omit comma, curly brace (indent)author.nameauthor["age"] Author.favoritecolor= "BLACK"// object traversal for k,v of author "My" +k+ "is" +v
Cycle:
// It is convenient to iterate through each element of the array animals=["dog", "cat", "Brid"] for in animals Console.log Animal
Function:
Anonymous functions:
// parameter is an anonymous function with name, or it can have multiple arguments. (name), return "hello,#{name}!" // after compilation (function(name) { return "Hello", "+name+"! " ; }) // function call sayhello= (name), return "hello,#{name}!" "Calamus"
Named functions: Most of the cases do not support named functions, the main reason is that IE's support for named functions is very weak, but can be used to bind a variable with a way to name the function
return value of the function
// If the function does not have an explicit return value, the last sentence is treated as the return value of the function about = (phrase), "{phrase.touppercase ()}!!!" // Return to confirm that no return value is added // null implicitly returns // compiled jsvarfunction (phrase) { return "" + (Phrase.touppercase ()) + "!!!" ;}
Class:
// declaration of a class Class Rabbit // create an instance of the class rabbit=new Rabbit () rabbit.color= "Gray"// to the class binding method, number is the parameter Class Rabbit Jump : (number) --"Grayrabbit"rabbit.jump ()
constructor function:
// just add constructor in front class Rabbit constructor: (Num,type= "Diesel"), @type=type; @number=number @load=0 @capacity=number * Other content
Inheritance of the class:
// practical extends indicates inheritance relationship Class Animal "Jump" class Rabbit extends animal//instance New Rabbit () Myrabbit.behavior ()
Coffeescript Grammar Summary