CoffeeScript简介 <二>
集合与迭代
.. 与 ...
先看例子:
arr = ["a1", "a2", "a3", "a4", "a5"]
arr[0..3] // ["a1", "a2", "a3", "a4"]
arr[-2..] // ["a4", "a5"]
arr[-3..3] // ["a3", "a4"]
arrRange = [1..5]//[1,2,3,4,5]
..包含右边区间。
arr = ["a1", "a2", "a3", "a4", "a5"]
arr[0...3] // ["a1", "a2", "a3"]
arr[-2...] // ["a4", "a5"]
arr[-3...3] // ["a3"]
arrRange = [1...5]//[1,2,3,4]
...不包含右边区间。
遍历
数组
arr = ["one", "two", "three", "four", "five"]
基本遍历:
console.log item for item in arr
加条件:
console.log item for item in arr when item isnt "two"
带索引:
console.log item for item,i in arr when i isnt 2
带循环项:
[1,5].map (i) -> console.log i*2 // 也可以用带索引的for循环
对象
obj = {a1: "a111", a2: "a222"}
console.log k,v for k,v of obj
循环
单行
console.log i for i in [0..5]
多行
for i in [0..5]
console.log i
条件语句
基本语法
eat food if cat is hungry
play game unless cat is hungry
play game if cat isnt hungry
生成js代码为:
if (cat === hungry) {
eat(food);
}
if (cat !== hungry) {
play(game);
}
if (cat !== hungry) {
play(game);
}
代码确实精简不少。
OO篇
使用coffeeScript实现面向对象写起来很爽。
类定义
class Animal
constructor: (name) ->
@name = name
sayhello: () ->
console.log @name
animal = new Animal('ray')
animal.sayhello()
生成的js代码为:
var Animal, animal;
Animal = (function() {
function Animal(name) {}
return Animal;
})();
this.name = name({
sayhello: function() {}
});
console.log(this.name);
animal = new Animal('ray');
animal.sayhello();
继承
class Animal
constructor: (name) ->
@name = name
sayhello: () ->
console.log @name
class Cat extends Animal
constructor:(name,@hungry) ->
super
CoffeeScript 与 jQuery
$(function(){})
|
|
$ ->
比如:
$(function() {
$('h1').click(function() {
$(this).html('I am clicked');
});
});
|
|
$ ->
$('h1').click ->
$(@).html 'I am clicked'
Require 与 Backbone
define [
'backbone'
'underscore'
'text!templates/yes.html'
], (Bacbone, _, tpl) ->
class UserView extends Backbone.View
events: {}
initialize: (options)->
render: ->
@$el.html _.template( tpl, { } )
其他
对象判空
console?.log? 'log'
if (typeof console !== "undefined" && console !== null) {
if (typeof console.log === "function") {
console.log('log');
}
}
关于 -> 和 => 号
cat = ->
console.log this
cat = =>
console.log @
# ->的结果
var cat;
cat = function() {
return console.log(this);
};
# => 的结果
cat = (function(_this) {
return function() {
return console.log(_this);
};
})(this);
也就是说:=>(胖头号)可以直接获取父级作用域中的this关键字。
最后
CoffeeScript简介 <二>的更多相关文章
- {Django基础七之Ajax} 一 Ajax简介 二 Ajax使用 三 Ajax请求设置csrf_token 四 关于json 五 补充一个SweetAlert插件(了解)
Django基础七之Ajax 本节目录 一 Ajax简介 二 Ajax使用 三 Ajax请求设置csrf_token 四 关于json 五 补充一个SweetAlert插件(了解) 一 Ajax简介 ...
- Windbg 脚本命令简介 二, Windbg command
Windbg 脚本命令简介 二, Windbg script command $<, $><, $$<, $$><, $$>a< (Run Scri ...
- {Django基础七之Ajax} 一 Ajax简介 二 Ajax使用 三 Ajax请求设置csrf_token 四 关于json 五 补充一个SweetAlert插件(了解)
{Django基础七之Ajax} 一 Ajax简介 二 Ajax使用 三 Ajax请求设置csrf_token 四 关于json 五 补充一个SweetAlert插件(了解) Django基础七之 ...
- WPF Binding值转换器ValueConverter使用简介(二)-IMultiValueConverter
注: 需要继承IMultiValueConverter接口,接口使用和IValueConverter逻辑相同. 一.MultiBinding+Converter 多值绑定及多值转换实例 当纵向流量大于 ...
- Hibernate框架简介(二)基本使用增、删、改、查
一.Hibernate框架简介 Hibernate是一个优秀的Java持久化层解决方案,是当今主流的对象-关系映射(ORM,ObjectRelationalMapping)工具 1.1.理解持久化 瞬 ...
- Selenium简介(二)--基于CORE/IDE的简单应用
参考 http://blog.csdn.net/iamqa/article/details/4398240 Selenium简介(一)--总体介绍 http://blog.csdn.net/iam ...
- Redis简介二
一.直接安装 1.Windows版本的Redis下载地址:https://github.com/dmajkic/redis/downloads ,选择一个你想要下载的版本下载即可~ ...
- RSA简介(二)——模幂算法
RSA最终加密.解密都要用到模乘的幂运算,简称模幂运算. 回忆一下RSA,从明文A到B B=Ae1%N 对B解密,就是 A=Be2%N 其中,一般来说,加密公钥中的e1一般会比较小,取65537居多, ...
- SystemVerilog语言简介(二)
6. 用户定义的类型 Verilog不允许用户定义新的数据类型.SystemVerilog通过使用typedef提供了一种方法来定义新的数据类型,这一点与C语言类似.用户定义的类型可以与其它数据类型一 ...
随机推荐
- 【C++ Primer | 15】构造函数与拷贝控制
合成拷贝控制与继承 #include <iostream> using namespace std; class Base { public: Base() { cout << ...
- Codeigniter使用HMVC(一)
涉及三方代码: https://tutorials.kode-blog.com/codeigniter-admin-panel https://bitbucket.org/wiredesignz/co ...
- JavaScript中为什么string可以拥有方法?
所有文章搬运自我的个人主页:sheilasun.me 引子 我们都知道,JavaScript数据类型分两大类,基本类型(或者称原始类型)和引用类型. 基本类型的值是保存在栈内存中的简单数据段,它们是按 ...
- python全栈开发day34-线程Thread
一.昨日内容回顾 1. 概念和理论 进程是计算机资源分配最小单位 进程三状态.同步.异步.阻塞.非阻塞 2. 进程的创建 实例化.自建类run,start,join,terminate,daemon等 ...
- metasploit常见服务的漏点扫描模块
弱点扫描 根据信息收集结果搜索漏洞利用模块 结合外部漏洞扫描系统对大IP地址段进行批量扫描 可以考虑对单个ip,单个服务进行扫描 NVC 密码破解端口:5900use auxiliary/scanne ...
- 18,EasyNetQ-使用替代DI容器
EasyNetQ由独立组件组成. 它在内部使用称为DefaultServiceProvider的小型内部DI(IoC)容器. 如果您查看用于创建核心IBus接口实例的静态RabbitHutch类的代码 ...
- C# JSON帮助类(可互转)
public class JsonHelper { public JsonHelper() { // // TODO: Add constructor logic here // } /// < ...
- HDU 5628 Clarke and math——卷积,dp,组合
HDU 5628 Clarke and math 本文属于一个总结了一堆做法的玩意...... 题目 简单的一个式子:给定$n,k,f(i)$,求 然后数据范围不重要,重要的是如何优化这个做法. 这个 ...
- GitLab 安装(推荐)
参考文档: https://about.gitlab.com/installation/#centos-7 基础环境 [root@node1 ~]# uname -r -.el7.x86_64 [ro ...
- Codeforces Round #519 by Botan Investments
Codeforces Round #519 by Botan Investments #include<bits/stdc++.h> #include<iostream> #i ...