当前位置: 首页 > news >正文

coffeescript 基本语法

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

在开始之前,coffeescript有一个TRY COFFEESCRIPT标签页。它会提供一个在线的coffeescript编译器。左边的是coffeescript代码,而右侧部分则是编译后的js代码。

代码注释

和Ruby注释的格式一样,以#开头,后面跟着注释内容即可

# A comment

当然,多行注释也是支持的,例如

###
  A multiline comment, perhaps a LICENSE.
###

变量和作用域

js有一个潜在的问题就是,当你忘记使用var声明变量的时候,这个变量的作用域就会被提升至全局作用域。

coffeescript修复了这个潜在的问题。coffeescript使用一个匿名函数把你的coffeescript代码包起来,然后所有的变量都使用var声明。

coffeescript代码

exports = this
exports.MyVariable = "foo-bar"

会被编译成:

var exports;
exports = this;
exports.MyVariable = "foo-bar";

不过当你需要设置全局变量的属性时,我建议你直接使用全局对象(比如在浏览器的环境下,全局对象就是window了)。

if/else

流程控制,最基本的应该就是if else 语句了:

if true == true
  "We're ok"
if true != true then "Panic"
# Equivalent to:
#  (1 > 0) ? "Ok" : "Y2K!"
if 1 > 0 then "Ok" else "Y2K!"

当然,实际开发过程中,if else 后面会跟着不止一样的表达式,这样的场景下,我们可以这么做:

if heat < 5
    #   do something
else
    #   do other things

更进一步的是,coffeescript还允许你像编写ruby代码一样,把if语句放在后面:

alert("It's cold!") if heat < 5

实际上这句代码会被翻译成:

if (heat < 5) {
  alert("It's cold!");
}

很神奇吧!我们来看另外一个js代码:

if (!true) {
  "Panic";
}

是的,js我们一般使用!做取反操作,但这样的代码其实偏向于给机器阅读,而不是说给人类阅读。换种方式:

if not true then "Panic"

是否好多了?其实我们还有另外一种表达方式:

"Panic" unless true

或者:

unless true
  "Panic"

不论是哪一种写法,结果都是一样的。unless === if not。

操作符和别名

其他的一些小的变化,比如is关键字会被翻译成===,isnt会被翻译成 !==等,虽然是微小的变化,但是在代码的可读性上却前进了一大步。

if true is 1
    "Type coercion fail!"

if true isnt true
    alert "Opposite day!"

拓展操作符

我们常常会有这么一个场景,就是判断一个变量是否为null或者undefined:

if !val
    //  do something

coffeescript为我们提供了个拓展操作符 ?

solipsism = true if mind? and not world?

等价于:

var solipsism;
if ((typeof mind !== "undefined" && mind !== null) && (typeof world === "undefined" || world === null)) {
  solipsism = true;
}

代码

footprints = yeti ? "bear"

会被翻译成:

var footprints;
footprints = typeof yeti !== "undefined" && yeti !== null ? yeti : "bear";

Switch/When/Else

switch day
  when "Mon" then go work
  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

Try/Catch/Finally

try
  allHellBreaksLoose()
  catsAndDogsLivingTogether()
catch error
  print error
finally
  cleanUp()

字符串

在ruby中,字符串支持内嵌变量,这是一个很好的语法层面的特性。coffeescript理所当然的借鉴了过来了:

author = "Wittgenstein"
quote  = "A picture is a fact. -- #{ author }"
sentence = "#{ 22 / 7 } is a decent approximation of π"

另外,字符串还有其他特性,比如multiline strings和block strings。

#   multiline strings
mobyDick = "Call me Ishmael. Some years ago --
 never mind how long precisely -- having little
 or no money in my purse, and nothing particular
 to interest me on shore, I thought I would sail
 about a little and see the watery part of the
 world..."

#   block strings
html = """
   <strong>
     cup of coffeescript
   </strong>
   """

block string在功能上,有点类似html中的<pre>标签。

我们经常会使用js的html模板方案去实现一些功能,使用string去拼接html代码实在是非常蹩脚的技巧。

这个时候如果html稍微长一些,我们要么使用array去join()得到一个html字符串或者直接通过字符串拼接。但不论哪一种方案,都没有coffeescript提供的这个block string来的优雅。

对象直接量

object3 = 
  one: 1
  two: 2

是的,和js的定义方式没多少区别。如果我们需要遍历对象原型链,在js中,我们的做法可以是:

var object = {one: 1, two: 2}
for(var key in object) alert(key + " = " + object[key])

但在coffeescript中,我们是这样做的:

for key, value of object
    #   do something

转载于:https://my.oschina.net/knightuniverse/blog/194145

相关文章:

  • ASP.NET MVC3-第02节-添加一个Controller (C#)
  • for test
  • What’s New in Python 2.7 — Python 3.4.0b2 documentation
  • C# 集合类 Array,Arraylist,List,Hashtable,Dictionary...
  • 转观念 变架构 补短板——析科华恒盛向数据中心方案商转型
  • spring security3.x学习(5)_如何拦截用户请求
  • php.ini中文解释
  • ubuntu下gvim字体设置
  • 《JAVA面向对象的特征 》
  • MySQL安装后设置root 密码
  • 对《神奇的C语言》文中例子 5 代码的分析讨论
  • Mysqlbackup 3.9.0 企业级备份工具详解
  • I/O负载均衡策略之一 条带化
  • 疯狂ios讲义之实现游戏逻辑(3)
  • 常见开源协议GPL、Apache、CDDL、BSD等区别
  • 《Javascript数据结构和算法》笔记-「字典和散列表」
  • Android交互
  • CentOS 7 修改主机名
  • extjs4学习之配置
  • JS正则表达式精简教程(JavaScript RegExp 对象)
  • node入门
  • Tornado学习笔记(1)
  • vagrant 添加本地 box 安装 laravel homestead
  • 基于阿里云移动推送的移动应用推送模式最佳实践
  • 如何利用MongoDB打造TOP榜小程序
  • 写代码的正确姿势
  • 学习笔记:对象,原型和继承(1)
  • 一起来学SpringBoot | 第十篇:使用Spring Cache集成Redis
  • 用简单代码看卷积组块发展
  • AI算硅基生命吗,为什么?
  • ​​​​​​​sokit v1.3抓手机应用socket数据包: Socket是传输控制层协议,WebSocket是应用层协议。
  • ​2021半年盘点,不想你错过的重磅新书
  • #Linux(帮助手册)
  • (1)SpringCloud 整合Python
  • (层次遍历)104. 二叉树的最大深度
  • (原创) cocos2dx使用Curl连接网络(客户端)
  • (原創) 是否该学PetShop将Model和BLL分开? (.NET) (N-Tier) (PetShop) (OO)
  • (转)C#开发微信门户及应用(1)--开始使用微信接口
  • .NET 8 中引入新的 IHostedLifecycleService 接口 实现定时任务
  • .net core 6 redis操作类
  • .NetCore Flurl.Http 升级到4.0后 https 无法建立SSL连接
  • .Net转前端开发-启航篇,如何定制博客园主题
  • /*在DataTable中更新、删除数据*/
  • /etc/fstab和/etc/mtab的区别
  • /使用匿名内部类来复写Handler当中的handlerMessage()方法
  • @Async注解的坑,小心
  • @data注解_一枚 架构师 也不会用的Lombok注解,相见恨晚
  • []AT 指令 收发短信和GPRS上网 SIM508/548
  • [2]十道算法题【Java实现】
  • [Angular] 笔记 21:@ViewChild
  • [BUAA软工]第一次博客作业---阅读《构建之法》
  • [C/C++]数据结构 堆的详解
  • [CF494C]Helping People
  • [C语言]——C语言常见概念(1)
  • [emuch.net]MatrixComputations(7-12)