{"action":"created","issue":{"url":"https://api.github.com/repos/mrdoob/three.js/issues/6362","labels_url":"https://api.github.com/repos/mrdoob/three.js/issues/6362/labels{/name}","comments_url":"https://api.github.com/repos/mrdoob/three.js/issues/6362/comments","events_url":"https://api.github.com/repos/mrdoob/three.js/issues/6362/events","html_url":"https://github.com/mrdoob/three.js/issues/6362","id":67222629,"number":6362,"title":"instanceof operator considered harmful","user":{"login":"kumavis","id":1474978,"avatar_url":"https://avatars.githubusercontent.com/u/1474978?v=3","gravatar_id":"","url":"https://api.github.com/users/kumavis","html_url":"https://github.com/kumavis","followers_url":"https://api.github.com/users/kumavis/followers","following_url":"https://api.github.com/users/kumavis/following{/other_user}","gists_url":"https://api.github.com/users/kumavis/gists{/gist_id}","starred_url":"https://api.github.com/users/kumavis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kumavis/subscriptions","organizations_url":"https://api.github.com/users/kumavis/orgs","repos_url":"https://api.github.com/users/kumavis/repos","events_url":"https://api.github.com/users/kumavis/events{/privacy}","received_events_url":"https://api.github.com/users/kumavis/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":24,"created_at":"2015-04-08T21:19:52Z","updated_at":"2015-05-02T18:00:04Z","closed_at":"2015-05-02T18:00:04Z","body":"The `instanceof` operator is a nice and easy way of doing class detection in javascript in a single project. However side effects creep in when we look at this at the ecosystem level.\r\n\r\nA project composed of three.js-based libraries will run into difficulties doing class detection. This is because they may have their own copies of the classes. One instance of Object3D may fail the `instanceof` test against the other module's Object3D class.\r\n\r\nThere are a number of issues originating from the use of this pattern:\r\n* https://github.com/mrdoob/three.js/issues/5673\r\n* https://github.com/mrdoob/three.js/issues/5886\r\n\r\nIt would also be a requirement for the proposed module-focused architecture change:\r\n* https://github.com/mrdoob/three.js/issues/4776\r\n\r\nThe primary solution is to moving to a type detection via feature detection [as proposed by @substack](https://github.com/mrdoob/three.js/issues/4776#issuecomment-42343615).\r\n\r\nThe usage would look something like this:\r\n```js\r\nExampleClass.instanceof( obj )\r\n```\r\nIt should return true if the obj appears to be an instance of the class or a subclass. It will perform the detection by checking for the presence of properties and methods on the obj associated with the class. We can still use `instanceof` to short circuit and fallback to feature detection for performance.\r\n```js\r\nExampleClass.instanceof = function(obj){\r\n  return (obj instanceof ExampleClass) || ( x in obj && y in obj )\r\n}\r\n```\r\nThis would become part of the standard three.js class definition pattern.\r\n\r\nPlease suggest any other approaches that could work.\r\n"},"comment":{"url":"https://api.github.com/repos/mrdoob/three.js/issues/comments/98382995","html_url":"https://github.com/mrdoob/three.js/issues/6362#issuecomment-98382995","issue_url":"https://api.github.com/repos/mrdoob/three.js/issues/6362","id":98382995,"user":{"login":"kumavis","id":1474978,"avatar_url":"https://avatars.githubusercontent.com/u/1474978?v=3","gravatar_id":"","url":"https://api.github.com/users/kumavis","html_url":"https://github.com/kumavis","followers_url":"https://api.github.com/users/kumavis/followers","following_url":"https://api.github.com/users/kumavis/following{/other_user}","gists_url":"https://api.github.com/users/kumavis/gists{/gist_id}","starred_url":"https://api.github.com/users/kumavis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kumavis/subscriptions","organizations_url":"https://api.github.com/users/kumavis/orgs","repos_url":"https://api.github.com/users/kumavis/repos","events_url":"https://api.github.com/users/kumavis/events{/privacy}","received_events_url":"https://api.github.com/users/kumavis/received_events","type":"User","site_admin":false},"created_at":"2015-05-02T18:00:04Z","updated_at":"2015-05-02T18:00:04Z","body":"@Quasimondo thats a weird one -- if it turns out to be actually related to three.js we can reopen this issue."}}