just half cup of coffee
来源:互联网 发布:色选机 知乎 编辑:程序博客网 时间:2024/06/05 07:16
排版若乱掉,参考https://www.zybuluo.com/bornkiller/note/35825。
前言
coffeescript
,某种程度上来说,只是一个单纯的预处理器,而其对javascript
编写带来的便利性甚至不如less
对于css
开发带来的效率提升。先说个人意见,不适合场景如下:
- 大比重
Nodejs
任务(参见class即可) - 团队皆具备扎实
javascript
功底,且引入jshint
等工具作规范检查
理由比较简单,nodejs
提供了类继承的方法(util.inherit
),使用coffee
的类反而啰嗦。coffee
本质上封装强制性的代码规范,对于团队中新老成员代码功力,规范意识参差不齐的情况,无疑可以减轻沟通的时间成本。但如果功力相当,无疑是道枷锁,限制开发人员的手脚。
参考
coffeescript
文档 http://coffeescript.org/
示例
数组,对象相关操作方法
coffee
的iteration
,主要用于数组与对象的遍历。示例为foreach
,map
, filter
的coffee
使用。相较来说,手写代码量更少。
movies = [ '500 days with summer' 'the dark knight' 'inception']###遍历,array.foreach映射,array.map筛选,array.filter###watch movie for movie in moviesmovieMap = (resolve movie for movie in movies)movieFilter = (movie for movie in movies when movie isnt 'inception')
编译后输出
var movie, movieFilter, movieMap, movies, _i, _len;movies = ['500 days with summer', 'the dark knight', 'inception'];/*遍历,array.foreach映射,array.map筛选,array.filter */for (_i = 0, _len = movies.length; _i < _len; _i++) { movie = movies[_i]; watch(movie);}movieMap = (function() { var _j, _len1, _results; _results = []; for (_j = 0, _len1 = movies.length; _j < _len1; _j++) { movie = movies[_j]; _results.push(resolve(movie)); } return _results;})();movieFilter = (function() { var _j, _len1, _results; _results = []; for (_j = 0, _len1 = movies.length; _j < _len1; _j++) { movie = movies[_j]; if (movie !== 'inception') { _results.push(movie); } } return _results;})();
同时引入Destructuring assignment
的赋值方式,感觉类似于占位符的工作机制, 数组利用对应的位置赋值,对象使用对应的key赋值,在类声明内部使用。
movies = [ '500 days with summer' 'the dark knight' 'inception']recommend = 'comedy': '500 days with summer' 'hero': 'the dark knight' 'fantasy':'inception'lists = 'youth is not time of life'[comedy, hero, fantasy] = movies{comedy, hero, fantasy} =recommend[first, ..., last] = lists.split(' ')
编译后代码为
var comedy, fantasy, first, hero, last, lists, movies, recommend, _ref;movies = ['500 days with summer', 'the dark knight', 'inception'];recommend = { 'comedy': '500 days with summer', 'hero': 'the dark knight', 'fantasy': 'inception'};lists = 'youth is not time of life';comedy = movies[0], hero = movies[1], fantasy = movies[2];comedy = recommend.comedy, hero = recommend.hero, fantasy = recommend.fantasy;_ref = lists.split(' '), first = _ref[0], last = _ref[_ref.length - 1];
类声明
选用coffee
后,不建议再通过直接构造函数的方式进行类声明,改而使用class
处理。声明一个Animal
类,再声明Cat
继承Animal
类。
class Animal constructor: (origin) -> @origin = originclass Cat extends Animal constructor: (origin) -> super origin @type = 'cat' @barkSound = "mew mew mew" bark: () -> console.log @barkSound ancestor: () -> console.log @origincat = new Cat 'reptile'cat.bark()cat.ancestor()
采用构造函数,如下实现
Animals = (origin) -> @origin = origin thisDog = (origin) -> Animals.call(this, origin) @type = 'dog' @barkSound = 'wang wang wang' thisDog.prototype.bark = () -> console.log @barkSoundDog.prototype.ancestor = () -> console.log @origindog = new Dog 'reptile'dog.bark()
容易造成混淆,所以既然采用coffee
,就用coffee
的推荐方案实现,混搭效果,无疑会造成潜在问题。
注意点
- 函数内部遍历时注意
return
返回值,如下情况编译结果明显差异
for movie in movies resolve(movie)sample = () -> for movie in movies resolve(movie)
- 括号的使用
括号之于我有两种用途,一种是无参数函数的调用
sample = () -> for movie in movies resolve(movie)sample()
另一种类似于四则运算不确定顺序时使用,在if
分支语句中需要注意。如下使用,去掉括号后,会把 _source && judge.isNumber value
结果作为judge.isNumber
的参数,不是期望的结果。
if (judge.isNumber _source) and (judge.isNumber value)
- webstorm
webstorm
支持coffee file watcher
,只需要安装coffee-script
之后,稍加配置,即可实时预览编译后结果,不建议通过grunt
,gulp
开watch
来预览。
后记
因为个人原因,不喜欢python,所以带着python影子的coffee也不是很喜欢。不过学习成本并不高,大概两天就可以流畅coding,在某些场合之下对团队开发效率提升有帮助,建议掌握。
- just half cup of coffee
- just half cup of coffee
- How to GET a Cup of Coffee
- Lesson Forty-Seven A cup of coffee. 一杯咖啡.
- Lesson 47 A cup of coffee 一杯咖啡
- Lesson 47 A cup of coffee 一杯咖…
- STRHH - Half of the half
- HDU1592-Half of and a Half
- WOJ1142-Half of and a Half
- FEW MORE CUPS OF COFFEE
- hdu 1592——Half of and a Half
- hdu 1592 Half of and a Half(大数)
- 杭电1592 Half of and a Half 大数
- 杭电1592Half of and a Half(大数)
- HDU 1592 Half of and a Half(高精度)
- Cup of Tea
- architecture of cup
- summary with him in half of year
- Java/Android 文件下载
- Android笔记----使用Intent和Android应用资源
- ITK配准:基于DeformationFieldTransform的配准
- js数据结构和算法(二)栈和队列
- js这些代码你都不会,你还有什么好说的!!!
- just half cup of coffee
- Leetcode@Search in Rotated Sorted Array
- linux下使用openssl对socket通信加密
- Linux下实现Mac下QuickSilver那样的一键切换程序(代替Alt-Tab键)
- Kafka#2:消息队列
- liunx学习在liunx 安装ffmpeg
- 如何在主框架类中获得视图类指针
- CentOS6.5搭建LNMP
- Mac平台上OpenCV开发环境搭建