{"action":"created","issue":{"url":"https://api.github.com/repos/unknownGe/unknownGe.github.io/issues/4","labels_url":"https://api.github.com/repos/unknownGe/unknownGe.github.io/issues/4/labels{/name}","comments_url":"https://api.github.com/repos/unknownGe/unknownGe.github.io/issues/4/comments","events_url":"https://api.github.com/repos/unknownGe/unknownGe.github.io/issues/4/events","html_url":"https://github.com/unknownGe/unknownGe.github.io/issues/4","id":111530293,"number":4,"title":"ES6","user":{"login":"unknownGe","id":4350418,"avatar_url":"https://avatars.githubusercontent.com/u/4350418?v=3","gravatar_id":"","url":"https://api.github.com/users/unknownGe","html_url":"https://github.com/unknownGe","followers_url":"https://api.github.com/users/unknownGe/followers","following_url":"https://api.github.com/users/unknownGe/following{/other_user}","gists_url":"https://api.github.com/users/unknownGe/gists{/gist_id}","starred_url":"https://api.github.com/users/unknownGe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/unknownGe/subscriptions","organizations_url":"https://api.github.com/users/unknownGe/orgs","repos_url":"https://api.github.com/users/unknownGe/repos","events_url":"https://api.github.com/users/unknownGe/events{/privacy}","received_events_url":"https://api.github.com/users/unknownGe/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-10-15T02:05:08Z","updated_at":"2015-10-25T04:41:33Z","closed_at":null,"body":"## 运行环境\r\n\r\n安装 node，并运行以下命令查看已经实现的ES6特性\r\n```sh\r\n$ node --v8-options | grep harmony\r\n```\r\n运行js文件\r\n```sh\r\n$ node --harmony --use-strict yourfilename.js\r\n```\r\n\r\n## let\r\n\r\n  1. let 声明的变量只在其所在的代码块中起作用。\r\n  2. 不存在变量提升，必须先声明后使用，不能重复声明。\r\n  3. 暂时性死区，在区块内形成封闭作用域，在let声明之前，外部同名变量也不能使用。 \r\n  4. 支持块级作用域\r\n\r\n```js\r\n{\r\n  {\r\n    /* 不存在变量提升，必须先声明后使用，不能重复声明  */\r\n    console.log(a); // ReferenceError\r\n\r\n    let a = 3;\r\n    console.log(a); // 3\r\n  }\r\n /* 支持块级作用域，let 声明的变量只在其所在的代码块中起作用*/\r\n  console.log(a); // ReferenceError\r\n}\r\n```\r\n```js\r\n{\r\n  var b = 1;\r\n  {\r\n   /* 暂时性死区，在区块内形成封闭作用域，在let声明之前，外部同名变量也不能使用 */\r\n    console.log(b); // ReferenceError\r\n\r\n    console.log(c); // undefined\r\n    let b = 2;\r\n    var c = 3;\r\n    console.log(b); // 2\r\n  }\r\n  console.log(b);  // 1\r\n  console.log(c);  // 3\r\n}\r\n```\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/unknownGe/unknownGe.github.io/issues/comments/150891537","html_url":"https://github.com/unknownGe/unknownGe.github.io/issues/4#issuecomment-150891537","issue_url":"https://api.github.com/repos/unknownGe/unknownGe.github.io/issues/4","id":150891537,"user":{"login":"unknownGe","id":4350418,"avatar_url":"https://avatars.githubusercontent.com/u/4350418?v=3","gravatar_id":"","url":"https://api.github.com/users/unknownGe","html_url":"https://github.com/unknownGe","followers_url":"https://api.github.com/users/unknownGe/followers","following_url":"https://api.github.com/users/unknownGe/following{/other_user}","gists_url":"https://api.github.com/users/unknownGe/gists{/gist_id}","starred_url":"https://api.github.com/users/unknownGe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/unknownGe/subscriptions","organizations_url":"https://api.github.com/users/unknownGe/orgs","repos_url":"https://api.github.com/users/unknownGe/repos","events_url":"https://api.github.com/users/unknownGe/events{/privacy}","received_events_url":"https://api.github.com/users/unknownGe/received_events","type":"User","site_admin":false},"created_at":"2015-10-25T04:41:33Z","updated_at":"2015-10-25T04:41:33Z","body":"与 C++ 的迭代器类似，ES6 也提供了 Iterator 接口，用来遍历一些数据结构。\r\n\r\n定义一个 Iterator 的方法如下：\r\n\r\n```TypeScript\r\n  // TypeScript  \r\n\r\n  interface IteratorResult {\r\n      done: boolean;\r\n      value: any;\r\n  }\r\n  interface Iterator {\r\n      next(): IteratorResult;\r\n  }\r\n```\r\n\r\nIterator 是一个对象，定义了用于遍历的 next 方法；next 返回的是一个对象，其中value是遍历的返回值，done表示遍历是否结束。\r\n\r\n```js\r\nfunction myIterator() {\r\n  var id = 0;\r\n  // 返回 Iterator\r\n  return ({\r\n    // 定义 next 方法\r\n    next() {  \r\n       if (id < 4) {\r\n         return {value: id++, done: false};\r\n       } else {\r\n         return {done: true};\r\n       }\r\n    }\r\n  });\r\n};\r\n\r\nvar it = myIterator();\r\nconsole.log(it.next()); // {value = 0, done = false }\r\n\r\n```\r\n\r\nIterator 的作用是遍历数据结构，为数据结构的访问提供统一的接口，那么如何遍历数据结构呢？首先要将 Iterator 部署到数据结构，部署方法如下：\r\n\r\n```TypeScript\r\ninterface Iterable {\r\n  [Symbol.iterator](): Iterator\r\n}\r\n```\r\n将 Iterator 配置给数据结构的 Symbol.iterator 属性，Symbol.iterator 是一个方法，调用这个方法就会得到 Iterator。\r\n\r\n```js\r\n\r\nvar array = [5,5,5,5,5];\r\n\r\n// 为array 配置 Iterator\r\narray[Symbol.iterator] = myIterator;\r\n\r\n```\r\n配置了 Symbol.iterator ，就可以使用 for...of 进行遍历。\r\n\r\n```js\r\nfor(var i of array) {\r\n  console.log(i); // 0 1 2 3\r\n}\r\n```\r\n一些原生的数据结构（例如Array，Set，Map）已经具备了Iterator接口，因为这些数据结构已经部署了 Symbol.iterator属性。在这里重新部署了 array 的 Symbol.iterator，因此for...of遍历array时没有打印它的元素。\r\n\r\nIterator 是为数据遍历服务的，更确切的说，是为for...of提供支持的；只有配置了Symbol.iterator，才能使用for...of遍历；另外，Object 是不支持 for...of 遍历的，可以使用for...in 。"}}