coffee 命令详解

| 选择喜欢的代码风格  

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 Logo

coffee 命令安装:


-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

coffee 命令补充说明:


CoffeeScript 是一种可以编译成 JavaScript 的小语言。在那种尴尬的 Java 式外表之下,JavaScript 一直有着一颗美丽的心。CoffeeScript 试图以一种简单的方式展示 JavaScript 的优点。

CoffeeScript 的黄金法则是: 它只是 JavaScript 代码一对一编译成等效的 JS,运行时无需解释。您可以从 CoffeeScript 无缝使用任何现有的 JavaScript 库(反之亦然)。编译后的输出可读性好、打印精美,并且运行速度往往与等效的手写 JavaScript 一样快甚至更快。

coffee 命令语法:


coffee
coffee [path/to/file.coffee]

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
 

用 CoffeeScript 开始制作一杯个“热咖啡”:


CoffeeScript variable


name = 'kamal'

CoffeeScript string


name = 'Mostafa kamal'
 career = "Developer | Designer"
html = """
       <strong>
         cup of coffeescript
       </strong>
       """

CoffeeScript interpolation


#{yo}

name = 'Mostafa Kamal'
 age = 5
 users = 'users name is #{name} and age is #{age}'

CoffeeScript functions


-> 是函数的关键词

myfuntion = (x) -> //something

CoffeeScript objects & array


lists = ['kamal', 'jamal', 5 , 'karim']
# objects
users = {name: "Mostafa", status: "Active"}
###
objects
###
users =
  infos:
    name: "Max"
    age:  11
  profile:
    status: "Developer"
    salary:  1000

CoffeeScript scoping


salary = 50000
users = ->
  salary = 60000
  payment = salary + 100

CoffeeScript conditions


if conditon
  x = 'this is if condition'
else if condtionElse
  x = 'This is else if condition'
else
  x = 'This is default condition'

  • tenray ? .. : ..

if conditionKamal then kamal else notKamal

CoffeeScript Loop


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

CoffeeScript switch / case


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

CoffeeScript Arrow function


Account = (customer, cart) ->
 @customer = customer
 @cart = cart
 $('.shopping_cart').on 'click', (event) =>
   @customer.purchase @cart

CoffeeScript class


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()

CoffeeScript Modules - ES6


import * as underscore from 'underscore'
export * from 'underscore'

CoffeeScript try / catch


connection(
  try
    'done'
  catch error
    "username or password mismatch ! check again"
)

CoffeeScript operators


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

coffee 命令扩展阅读:




coffee 命令评论