{"action":"created","issue":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/9630","repository_url":"https://api.github.com/repos/Microsoft/TypeScript","labels_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/9630/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/9630/comments","events_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/9630/events","html_url":"https://github.com/Microsoft/TypeScript/issues/9630","id":164992324,"number":9630,"title":"Transpiling mess up the lexical scoping of arrow function","user":{"login":"smangalath","id":3512777,"avatar_url":"https://avatars.githubusercontent.com/u/3512777?v=3","gravatar_id":"","url":"https://api.github.com/users/smangalath","html_url":"https://github.com/smangalath","followers_url":"https://api.github.com/users/smangalath/followers","following_url":"https://api.github.com/users/smangalath/following{/other_user}","gists_url":"https://api.github.com/users/smangalath/gists{/gist_id}","starred_url":"https://api.github.com/users/smangalath/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/smangalath/subscriptions","organizations_url":"https://api.github.com/users/smangalath/orgs","repos_url":"https://api.github.com/users/smangalath/repos","events_url":"https://api.github.com/users/smangalath/events{/privacy}","received_events_url":"https://api.github.com/users/smangalath/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/Microsoft/TypeScript/labels/Needs%20More%20Info","name":"Needs More Info","color":"556b75"}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2016-07-12T04:34:45Z","updated_at":"2016-07-12T21:59:53Z","closed_at":null,"body":"<!-- BUGS: Please use this template. -->\r\n<!-- QUESTIONS: This is not a general support forum! Ask Qs at http://stackoverflow.com/questions/tagged/typescript -->\r\n<!-- SUGGESTIONS: See https://github.com/Microsoft/TypeScript-wiki/blob/master/Writing-Good-Design-Proposals.md -->\r\n\r\n**TypeScript Version:**  1.8.0 / nightly (2.0.0-dev.201xxxxx)\r\n\r\n**Code**\r\n```ts\r\nvar CounterES6 = ()=>{\r\n    console.log('Inside CounterES6')\r\n    this.seconds = 0;\r\n    setInterval(()=>{\r\n        console.log('setInterval ' + this.seconds);\r\n        this.seconds++;\r\n\r\n    },1000)\r\n}\r\n\r\nvar counterB = new CounterES6();\r\nsetTimeout(()=>{\r\n    console.log('setTimeout '+ counterB.seconds);\r\n},1000);\r\n```\r\n// A *self-contained* demonstration of the problem follows...\r\n\r\nResult\r\nInside CounterES6\r\nsetInterval 0\r\nsetTimeout undefined\r\nsetInterval 1\r\nsetInterval 2\r\nsetInterval 3\r\nsetInterval 4\r\nsetInterval 5\r\n\r\nReason\r\nTranspiled code doesn't store 'this' properly\r\n```js\r\n//ES6\r\nvar CounterES6 = function () {\r\n    console.log('Inside CounterES6');\r\n    _this.seconds = 0;\r\n    setInterval(function () {\r\n        console.log('setInterval ' + _this.seconds);\r\n        _this.seconds++;\r\n    }, 1000);\r\n};\r\nvar counterB = new CounterES6();\r\nsetTimeout(function () {\r\n    console.log('setTimeout ' + counterB.seconds);\r\n}, 1000);\r\n```\r\n**Expected behavior:**\r\n```js\r\nvar _this = this; // to be there\r\n```\r\n**Actual behavior:** \r\n"},"comment":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/comments/232194985","html_url":"https://github.com/Microsoft/TypeScript/issues/9630#issuecomment-232194985","issue_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/9630","id":232194985,"user":{"login":"mhegazy","id":8000722,"avatar_url":"https://avatars.githubusercontent.com/u/8000722?v=3","gravatar_id":"","url":"https://api.github.com/users/mhegazy","html_url":"https://github.com/mhegazy","followers_url":"https://api.github.com/users/mhegazy/followers","following_url":"https://api.github.com/users/mhegazy/following{/other_user}","gists_url":"https://api.github.com/users/mhegazy/gists{/gist_id}","starred_url":"https://api.github.com/users/mhegazy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mhegazy/subscriptions","organizations_url":"https://api.github.com/users/mhegazy/orgs","repos_url":"https://api.github.com/users/mhegazy/repos","events_url":"https://api.github.com/users/mhegazy/events{/privacy}","received_events_url":"https://api.github.com/users/mhegazy/received_events","type":"User","site_admin":false},"created_at":"2016-07-12T21:59:53Z","updated_at":"2016-07-12T21:59:53Z","body":"this is not correct. as per the ES6 spec, lambda's do not have their own `this` binding. and inherit that of the containing  scope. in this case the scope is the global, and there is not much the compiler can do for that."}}
{"actor":{"display_login":"mhegazy"}}