zweizhao.github.io

项目,文章,随笔,博客,Markdown,个人网站,人生感悟。

View on GitHub

logic 为判断语句,如:right1 and/or/isnt right2

判断

do... if right # 单行

# 常规
if logic
	do...
else if logic
	do...
...
else
	do...

# 三目运算符
val = if right then a else b

循环

# of 用来遍历 Object,in 用来遍历数组
do... for element in arr
do... for element, index in arr
do... for element in arr when logic
do... for element, index in arr when logic
do... for key, value of obj

# range
do... for number in [num1..num2]
do... for number in [num1...num2]
do... for number in [num1..num2] by step
do... for number in [num1...num2] by step

# 过滤
filters = ele for ele in arr when logic # 返回最后一个
filters = (ele for ele in arr when logic) # 返回迭代
filters = for ele in arr when logic
	ele # 效果同上

# 实例
yearsOld = max: 10, ida: 9, tim: 11
as = "#{k}#{v}" for k, v of yearsOld
console.log as
# tim11

as = ("#{k}#{v}" for k, v of yearsOld)
console.log as
# [ 'max10', 'ida9', 'tim11' ]

as =  for k, v of yearsOld
	"#{k}#{v}"
console.log as
# [ 'max10', 'ida9', 'tim11' ]

as = i for i in [0..10]
console.log as
# 10

as = (i for i in [0..10])
console.log as
# [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]

as = for i in [0..10]
	i
console.log as
# [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]

# while
do...  while logic
do... until logic