{"action":"opened","issue":{"url":"https://api.github.com/repos/KeesCBakker/Strongly-Typed-Events-for-TypeScript/issues/28","repository_url":"https://api.github.com/repos/KeesCBakker/Strongly-Typed-Events-for-TypeScript","labels_url":"https://api.github.com/repos/KeesCBakker/Strongly-Typed-Events-for-TypeScript/issues/28/labels{/name}","comments_url":"https://api.github.com/repos/KeesCBakker/Strongly-Typed-Events-for-TypeScript/issues/28/comments","events_url":"https://api.github.com/repos/KeesCBakker/Strongly-Typed-Events-for-TypeScript/issues/28/events","html_url":"https://github.com/KeesCBakker/Strongly-Typed-Events-for-TypeScript/issues/28","id":448935520,"node_id":"MDU6SXNzdWU0NDg5MzU1MjA=","number":28,"title":"Add support for `thisArg`","user":{"login":"babakks","id":36728931,"node_id":"MDQ6VXNlcjM2NzI4OTMx","avatar_url":"https://avatars0.githubusercontent.com/u/36728931?v=4","gravatar_id":"","url":"https://api.github.com/users/babakks","html_url":"https://github.com/babakks","followers_url":"https://api.github.com/users/babakks/followers","following_url":"https://api.github.com/users/babakks/following{/other_user}","gists_url":"https://api.github.com/users/babakks/gists{/gist_id}","starred_url":"https://api.github.com/users/babakks/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/babakks/subscriptions","organizations_url":"https://api.github.com/users/babakks/orgs","repos_url":"https://api.github.com/users/babakks/repos","events_url":"https://api.github.com/users/babakks/events{/privacy}","received_events_url":"https://api.github.com/users/babakks/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2019-05-27T16:39:18Z","updated_at":"2019-05-27T16:39:18Z","closed_at":null,"author_association":"NONE","body":"Due to the nature of JavaScript, method contexts (i.e., `this` reference) could change depending on the way of invocation. In STE, using `subscribe` or `sub` without binding the listener method to the calling context would result in unexpected results.\r\n\r\nFor example:\r\n```ts\r\nimport { ISimpleEvent, SimpleEventDispatcher } from \"ste-simple-events\";\r\n\r\nconst event = new SimpleEventDispatcher();\r\nconst foo = new Foo();\r\nevent.subscribe(foo.bar); // Dynamic-context subscription.\r\nevent.subscribe(foo.bar.bind(foo)); // Static-context subscription.\r\nevent.dispatch();\r\n\r\nclass Foo {\r\n  dummy = 1;\r\n  bar() { console.log(this.dummy); }\r\n}\r\n```\r\n\r\nHere, the first subscription results in unexpected behavior due to its dynamic context, however, this is not totally unexpected due to the nature of the language.\r\n\r\nCurrently I'm using the binding method (second subscription), but it would be better for STE to take another argument as `thisArg` to hold the context and do the binding behind the scene, which complies with the current well-known conventions in the JS/TS community. Obviously, the argument is meant to be optional.\r\n"}}
{"actor":{"display_login":"babakks"}}