# 1：CoffeeScript初学者

## 数学

CoffeeScript支持所有你熟悉的数学运算符号：+ - ＊ /（加，减，乘，除），加一个你或许不熟悉的：%或取模。在原型交互时你将要做的最多的就是简单的数学运算。

#### 符号运算规则

``````10 + 20
# => 25

150 - 5 * 20
# => 50

(150 - 5) * 20
# => 2900
``````

## 数据的类型

#### 字符串

``````"This is a string"
'This is also a string'
``````

``````"My name is " + "Tessa"
# => "My name is Tessa"
``````

``````"50" + "50"
# => "5050"
``````

``````"50" + 50
# => "5050"
``````

## 使用变量

``````name = "Tessa"
print name
# =>"Tessa"

age = 26
print age
# => 26
``````

``````color = "green"

print "My favorite color is " + color
# => "My favorite color is green"

color = "red"

print "My favorite color is " + color
# => "My favorite color is red"
``````

• 变量名不能包含空格
• 变量名不能数字开头
• 变量名不能包含标点和 _ 以外的字符
• 变量名可以包含大写和小写字母

``````my_name = "Tessa"
myName = "Tessa"
``````

#### 字符串内插

``````color = "green"

print "my favorite color is " + color + ",what's yours?"
``````

``````color = "green"
print "my favorite color is #{color}, what's yours?"
# =>"my favorite color is green,what's yours?"
``````

## 布尔值

``````myBoolean = true
``````

``````10>9
# => true

9<8
# => false
``````

``````theTruth = 10 < 5

print theTruth
# => false
``````

#### 同等比较

``````5 is 5
# => true

num = 5
num is 5
# => true

num = 10
# => false
``````

``````num = 5
num isnt 10
# => true
``````

## 条件语句

``````num = 4

if num >= 16
print "you can learn to drive"
else
print "you're too young to learn to drive"

# => "you're too young to learn to drive"
``````

## 注释

``````# this is a comment. It doesn't do anything. But it's nice to read.
``````

## 函数

``````checkAge =
``````

``````checkAge = ->
# 我们将把检查年龄的代码放这
``````

``````checkAge = ->
# 还不能做任何事

checkAge()
``````

（）部分基本上是指“go”。它在告诉计算机在年龄检查变量上运行函数。

``````checkAge = ->
if age >= 16
print "carry on"
else
print "get out of the car please"
``````

``````age = 16

checkAge()

# which will output "carry on" because we set the age variable to 16
``````

``````age = 15
checkAge()
# => "Get out of the car please"

age = 18
checkAge()
# =>"Carry on"
``````

#### 带参数的函数

``````checkAge=(age) ->
if age >= 16
print "Carry on"
else
print "Get out of the car please"
``````

``````checkAge(17)
# =>"Carry on"
``````

``````patrol = (age,speed) ->
if speed > 60
if age >= 16
print"Happy speed ticket"
else
print"Get out of the car,kid"
``````

``````patrol(17,70)
# => "Happy speeding ticket"

patrol(17,40)
# =>...(nothing happens)

patrol(15,90)
# =>"Get out of the car,kid"
``````

``````patrol(16)
# =>undefined
``````

#### 使用预写函数

``````licensePlateCheck("BAD455")

# => true
``````

## 其他数据类型

#### 数组（array）

``````fruits = ["apples","oranges","bananas"]
``````

``````fruits.length
# => 3
``````

``````print fruits[1]

# => "oranges"
``````

#### 添加到数组

``````fruits.push("kiwis")

print fruits

# => ["apples","oranges","bananas","kiwis"]
``````

#### 通过数组循环

CoffeeScript中的数组是非常强大的，因为你可以通过数组中不同的项目一边又一遍的重复特定的功能。

toUpperCase 是一种字符串属性，可以将其变为大写形式。如果我们想要将水果数组中的每一个元素变为大写字母，我们可以：

``````fruits[0].toUpperCase()
# => "APPLES"

fruits[1].toUpperCase()
# => "ORANGES"

fruits[2].toUpperCase()
# => "BANANAS"
``````

``````for fruits in fruits
fruit.toUpperCase()

# => "APPLES"
# => "ORANGES"
# => "BANANAS"
``````

``````plates = ["BRR010","BUU888","NNB001","MBB991"]
``````

``````for plate in plates

# => false
# => true
# => false
``````

#### 对象

``````book =
title:"Slaughterhouse Five"
author:"Kurt Vonnegut"
genre:"Science Fiction"

``````

``````book =
pages:256
genres:["Science Fiction","Satire"]
fiction:true
``````

``````book =
author:
name:"Kurt Vonnegut"
born:1992
died:2007
``````

``````print book.title

# => "Slaughterhouse Five"

print book.pages

# => 1922
``````

``````print book.author.born
# => 1922
``````

``````print book.genres[1]
# => "Satire"
``````

## 方法（Methods）

``````book =
title:"Slaugherhouse Five"
print "All this happend,more or less"
``````

``````book.read()

# =>"All this happended,more or less."
``````

#### 配置对象

``````box =
width:120
height:120
x:0
y:0

animation =
duration:300
easing:"ease-in"
``````

### 推荐阅读更多精彩内容

• 第5章 引用类型(返回首页) 本章内容 使用对象 创建并操作数组 理解基本的JavaScript类型 使用基本类型...
大学一百阅读 1,272评论 0 4
• 指针是Ｃ语言中广泛使用的一种数据类型。 运用指针编程是Ｃ语言最主要的风格之一。利用指针变量可以表示各种数据结构； ...
朱森阅读 2,448评论 3 44
• 使用iTunes、iTools、PP助手类似软件下载iOS项目的ipa包 解压下载好的ipa包 进入解压后的文件夹...
沈正方阅读 733评论 0 0
• 窗外，淅淅沥沥的下着小雨，阴沉的天幕似乎在诉说着什么。不知道此刻在雨中踌躇的人们，是忘了打伞，还是在倾听着雨的细语...
Allen赵阅读 93评论 0 0
• 123
明明白白我的鑫阅读 37评论 0 0