> For the complete documentation index, see [llms.txt](https://zurassic.gitbook.io/10x-developer/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://zurassic.gitbook.io/10x-developer/javascript/coffeescript_to_es6.md).

# CoffeeScript to ES6 cheatsheet

References:

* <https://gist.github.com/danielgtaylor/0b60c2ed1f069f118562>
* <https://robots.thoughtbot.com/replace-coffeescript-with-es6>

## Iteration

* On object properties:

```javascript
// coffee
for prop, value of obj

// ES6
_.each(obj, (value, key)=>{
});

// or (NOTE don't use it for array)
for (let prop in obj) {
}
```

* On collection (array):

```javascript
// coffee
for ele in arr
    console.log ele

// es6
for (let ele of arr) {
    console.log(ele);
}

// or
arr.forEach( (ele) => {
    console.log(ele);
    //  函数体内的this对象，就是定义时所在的对象，而不是使用时所在的对象
});

// or
arr.forEach(function(ele){
    console.log(ele);
    //  this is the function
});
```

## Misc

* Replace `==`/`!=` with `===`/`!==`
* [There is no concept of class variable in ES6](http://stackoverflow.com/questions/22528967/es6-class-variable-alternatives), you might want to just turn that to a static method:

```javascript
// coffee
class MyFile
    @EXT = '.myfile'

// es6
class MyFile {
  static Ext() {
    return '.myfile';
  }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://zurassic.gitbook.io/10x-developer/javascript/coffeescript_to_es6.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
