- CoffeeScript 教程
- CoffeeScript - 主頁
- CoffeeScript - 概覽
- CoffeeScript - 環境
- CoffeeScript - 命令列實用程式
- CoffeeScript - 語法
- CoffeeScript - 資料型別
- CoffeeScript - 變數
- CoffeeScript - 運算子和別名
- CoffeeScript - 條件語句
- CoffeeScript - 迴圈
- CoffeeScript - 解析
- CoffeeScript - 函式
- 面向物件 CoffeeScript
- CoffeeScript - 字串
- CoffeeScript - 陣列
- CoffeeScript - 物件
- CoffeeScript - 範圍
- CoffeeScript - Splat
- CoffeeScript - 日期
- CoffeeScript - 數學
- CoffeeScript - 異常處理
- CoffeeScript - 正則表示式
- CoffeeScript - 類和繼承
- 高階 CoffeeScript
- CoffeeScript - Ajax
- CoffeeScript - jQuery
- CoffeeScript - MongoDB
- CoffeeScript - SQLite
- CoffeeScript 有用資源
- CoffeeScript - 快速指南
- CoffeeScript - 有用資源
- CoffeeScript - 討論
CoffeeScript - 針對 for..of 的解析
與陣列一樣,CoffeeScript 提供了 容器 來儲存稱為 物件 的鍵值對。我們可以使用 CoffeeScript 提供的 for..of 解析來迭代物件。
語法
假設我們在 CoffeeScript 中有一個物件形式為 { key1: value, key2: value, key3: value},然後你可以使用 for..of 解析來迭代這些元素,如下所示。
for key,value of { key1: value, key2: value, key3: value}
console.log key+"::"+value
示例
以下示例演示了 CoffeeScript 提供的 for..of 解析的用法。將此程式碼儲存到名為 for_of_example.coffee 的檔案中
for key,value of { name: "Mohammed", age: 24, phone: 9848022338}
console.log key+"::"+value
開啟 命令提示符 並編譯 .coffee 檔案,如下所示。
c:\> coffee -c for_of_example.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_example.coffee
在執行時,CoffeeScript 檔案會生成以下輸出。
name::Mohammed age::24 phone::9848022338
注意 − 我們將在本教程後續的各個章節中詳細討論陣列、物件和範圍。
coffeescript_comprehensions.htm
廣告