Last active
August 29, 2015 14:09
-
-
Save jiangtao/02b8fe61deab901813d6 to your computer and use it in GitHub Desktop.
coffee script 学习笔记
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
# 变量 | |
number = 2 | |
str = '4' | |
number = 1 if str | |
# 变量 数组和objecet处理 | |
[a, b, c] = [1, 2, 3] | |
{key1, key2} = {key1: 'a', key2: 'b'} | |
# 方法 | |
fn = (x) -> x*x | |
# 数组 | |
list = [1,2,3,4,5] | |
# 对象 | |
config = | |
prefix : 'task' | |
fn: fn | |
cube: (x) -> x*x | |
# 方法 多参数 | |
handle = (x, y) -> | |
console.log x,y | |
# 方法 多参数 arguments | |
handle2 = (x, y...) -> | |
print x, y | |
# 判断是否为空 | |
alert 'I knew it!' if number? | |
# 数组遍历 | |
cubes = (config.cube number for num in list) | |
# 数组遍历 ES5 | |
# 区别下面两种 | |
# 正确写法 方法后加空格表示以参数形式传入 | |
cubes = list.map (ele) -> | |
ele * ele | |
# 错误写法 | |
cubes = list.map(x) -> | |
x * x | |
# coffee script 一些技巧写法 | |
numbers = [1,2,3,4,5] | |
# 从0开始取2个 包含第2个 | |
start = numbers[0..2] | |
# 从0开始取2个 不包含第2个 | |
start = numbers[0...2] | |
# 复制 | |
copy = numbers[..] | |
# 替换数组从3-5 包含5的值 | |
numbers[3..5] = ['x', 'y', 'z'] | |
# 替换数组从3-5 不包含5的值 | |
numbers[3...5] = ['x', 'y'] | |
# 替换数组 | |
# target: Array | |
# replate: Array | |
# start: init | |
replaceArr = (target, replace, start) -> | |
end = start + replace.length | |
target[start..end] = replace | |
target | |
# 表达式 | |
getTask = (config) -> | |
if config.task | |
'getTask' | |
else if config.project | |
pid = config.project.id | |
if pid | |
then pid | |
else 'getProject' | |
# 遍历window 获取globals的前10项 | |
globales = (name for name of window)[0..10] | |
# class | |
class Point | |
constructor: (@name) -> | |
move: (meters) -> | |
alert @name + "moved #{meters}" | |
@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment