- CoffeeScript 教程
- CoffeeScript - 首页
- CoffeeScript - 概述
- CoffeeScript - 环境
- CoffeeScript - 命令行工具
- CoffeeScript - 语法
- CoffeeScript - 数据类型
- CoffeeScript - 变量
- CoffeeScript - 运算符和别名
- CoffeeScript - 条件语句
- CoffeeScript - 循环
- CoffeeScript - 推导式
- CoffeeScript - 函数
- CoffeeScript 面向对象编程
- CoffeeScript - 字符串
- CoffeeScript - 数组
- CoffeeScript - 对象
- CoffeeScript - 范围
- CoffeeScript - 展开运算符
- CoffeeScript - 日期
- CoffeeScript - 数学
- CoffeeScript - 异常处理
- CoffeeScript - 正则表达式
- CoffeeScript - 类和继承
- CoffeeScript 高级
- CoffeeScript - Ajax
- CoffeeScript - jQuery
- CoffeeScript - MongoDB
- CoffeeScript - SQLite
- CoffeeScript 有用资源
- CoffeeScript - 快速指南
- CoffeeScript - 有用资源
- CoffeeScript - 讨论
CoffeeScript - 后缀表达式推导
就像后缀形式的if和unless一样,CoffeeScript也提供了推导式的后缀形式,这在编写代码时非常方便。使用它,我们可以将for..in推导式写成一行代码,如下所示。
#Postfix for..in comprehension
console.log student for student in ['Ram', 'Mohammed', 'John']
#postfix for..of comprehension
console.log key+"::"+value for key,value of { name: "Mohammed", age: 24, phone: 9848022338}
后缀形式的for..in推导式
以下示例演示了CoffeeScript提供的for..in推导式的后缀形式的用法。将此代码保存在名为for_in_postfix.coffee的文件中。
console.log student for student in ['Ram', 'Mohammed', 'John']
打开命令提示符并编译.coffee文件,如下所示。
c:\> coffee -c for_in_postfix.coffee
编译后,它会生成以下JavaScript代码。
// Generated by CoffeeScript 1.10.0
(function() {
var i, len, ref, student;
ref = ['Ram', 'Mohammed', 'John'];
for (i = 0, len = ref.length; i < len; i++) {
student = ref[i];
console.log(student);
}
}).call(this);
现在,再次打开命令提示符并运行CoffeeScript文件,如下所示。
c:\> coffee for_in_postfix.coffee
执行后,CoffeeScript文件会产生以下输出。
Ram Mohammed John
后缀形式的for..of推导式
以下示例演示了CoffeeScript提供的for..of推导式的后缀形式的用法。将此代码保存在名为for_of_postfix.coffee的文件中。
console.log key+"::"+value for key,value of { name: "Mohammed", age: 24, phone: 9848022338}
打开命令提示符并编译.coffee文件,如下所示。
c:\> coffee -c for_of_postfix.coffee
编译后,它会生成以下JavaScript代码。
// Generated by CoffeeScript 1.10.0
(function() {
var key, ref, value;
ref = {
name: "Mohammed",
age: 24,
phone: 9848022338
};
for (key in ref) {
value = ref[key];
console.log(key + "::" + value);
}
}).call(this);
现在,再次打开命令提示符并运行CoffeeScript文件,如下所示。
c:\> coffee for_of_postfix.coffee
执行后,CoffeeScript文件会产生以下输出。
name::Mohammed age::24 phone::9848022338
coffeescript_comprehensions.htm
广告