A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
CoffeeScript
脚本或将其编译为 JavaScript
。
-bash/zsh: coffee command not found # Windows (WSL2) sudo apt-get update sudo apt-get install coffeescript # Debian apt-get install coffeescript # Ubuntu apt-get install coffeescript # Arch Linux pacman -S coffeescript # Kali Linux apt-get install coffeescript # Fedora dnf install coffeescript # OS X brew install coffeescript # Raspbian apt-get install coffeescript # Dockerfile dockerfile.run/coffee
CoffeeScript 是一种可以编译成 JavaScript 的小语言。在那种尴尬的 Java 式外表之下,JavaScript 一直有着一颗美丽的心。CoffeeScript 试图以一种简单的方式展示 JavaScript 的优点。
CoffeeScript 的黄金法则是: 它只是 JavaScript 代码一对一编译成等效的 JS,运行时无需解释。您可以从 CoffeeScript 无缝使用任何现有的 JavaScript 库(反之亦然)。编译后的输出可读性好、打印精美,并且运行速度往往与等效的手写 JavaScript 一样快甚至更快。
coffee coffee [path/to/file.coffee]
运行 coffee 脚本:
coffee path/to/file.coffee
coffee 编译为 JavaScript 并保存到同名文件中:
coffee --compile path/to/file.coffee
coffee 编译为 JavaScript 并保存到给定的输出文件:
coffee --compile path/to/file.coffee --output path/to/file.js
coffee 启动 REPL - 交互式 Shell:
coffee --interactive
coffee 观察脚本的变化并重新运行脚本:
coffee --watch path/to/file.coffee
name = 'kamal'
name = 'Mostafa kamal' career = "Developer | Designer" html = """ <strong> cup of coffeescript </strong> """
#{yo}
name = 'Mostafa Kamal' age = 5 users = 'users name is #{name} and age is #{age}'
-> 是函数的关键词
myfuntion = (x) -> //something
lists = ['kamal', 'jamal', 5 , 'karim'] # objects users = {name: "Mostafa", status: "Active"} ### objects ### users = infos: name: "Max" age: 11 profile: status: "Developer" salary: 1000
salary = 50000 users = -> salary = 60000 payment = salary + 100
if conditon x = 'this is if condition' else if condtionElse x = 'This is else if condition' else x = 'This is default condition'
if conditionKamal then kamal else notKamal
items = ['toast', 'cheese', 'wine'] eat food for food in items # [..] to <- from countdown = (num for num in [10..1]) if price == 500 buy() while supply > demand sell() until supply > demand
switch day when "Mon" then { test: 'kamal' } when "Tue" then go relax when "Thu" then go iceFishing when "Fri", "Sat" if day is bingoDay go bingo go dancing when "Sun" then go church else go work
Account = (customer, cart) -> @customer = customer @cart = cart $('.shopping_cart').on 'click', (event) => @customer.purchase @cart
class Animal constructor: (@name) -> move: (meters) -> alert @name + " moved #{meters}m." class Snake extends Animal move: -> alert "Slithering..." super 5 class Horse extends Animal move: -> alert "Galloping..." super 45 sam = new Snake "Sammy the Python" tom = new Horse "Tommy the Palomino" sam.move() tom.move()
import * as underscore from 'underscore' export * from 'underscore'
connection( try 'done' catch error "username or password mismatch ! check again" )
coffee | JS |
---|---|
is | === |
isnt | != |
not | ! |
and | && |
or | // {or} |
true, yes, on | true |
false, no, off | false |
@, this | this |
a ** b | Math.pow(a, b) |
a // b | Math.floor(a / b) |
a %% b | (a % b + b) % b |