{"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":8,"created_at":"2015-10-15T02:05:08Z","updated_at":"2015-11-17T13:46:46Z","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/157374073","html_url":"https://github.com/unknownGe/unknownGe.github.io/issues/4#issuecomment-157374073","issue_url":"https://api.github.com/repos/unknownGe/unknownGe.github.io/issues/4","id":157374073,"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-11-17T13:46:46Z","updated_at":"2015-11-17T13:46:46Z","body":"## Decorators\r\n由Yehuda Katz 提出的 [Decorators](https://github.com/wycats/javascript-decorators) ，是ES7的一个提案。\r\n> Decorators make it possible to annotate and modify classes and properties at design time.\r\n> A decorator is:\r\n> + an expression\r\n> + that evaluates to a function\r\n> + that takes the target, name, and decorator descriptor as arguments\r\n> + and optionally returns a decorator descriptor to install on the target object\r\n\r\nDecorators 作为一种语法糖，可以修饰类和类的属性，但是要依赖于 ES5 的 Object.defineProperty。Decorators 修饰属性的时候， 传递的参数 target、 name、 decorator 与 Object.defineProperty 的参数是一致的，target 是类的 prototype。\r\n```js\r\nfunction setLog(target, name, descriptor) {\r\n  let funName = '_' + name;\r\n  Object.defineProperty(target, funName, {\r\n    value: function() {\r\n        console.log('start run ' + name);\r\n        target[name]();\r\n        console.log(name + ' finish');\r\n      }\r\n  });\r\n  return descriptor;\r\n}\r\nclass Apple {\r\n  @setLog\r\n  showName() {\r\n    console.log('apple');\r\n  }\r\n}\r\n\r\nvar apple = new Apple();\r\napple._showName();\r\n```\r\nDecorators 也可以修饰类，参数 target 为类的自身。\r\n ```js\r\nfunction isFruit(target) {\r\n  target.isFruit = true;\r\n}\r\n\r\n@isFruit\r\nclass Apple {\r\n}\r\nconsole.log(Apple.isFruit); // true\r\n```\r\n\r\n目前 babel 支持 decorators，使用方法如下：\r\n命令行：\r\n```\r\n$ babel --optional es7.decorators source.js > dest.js\r\n``` \r\n脚本：\r\n```js\r\nbabel.transform(\"code\", {optional: [\"es7.decorators\"]})\r\n```\r\n\r\nrefer:\r\n\r\n[Javascript Decorators](https://github.com/wycats/javascript-decorators)\r\n[Decorators in ES7](http://hackll.com/2015/07/24/decorators-in-es7/)\r\n\r\n"}}