{"action":"created","issue":{"url":"https://api.github.com/repos/mbostock/d3/issues/2539","labels_url":"https://api.github.com/repos/mbostock/d3/issues/2539/labels{/name}","comments_url":"https://api.github.com/repos/mbostock/d3/issues/2539/comments","events_url":"https://api.github.com/repos/mbostock/d3/issues/2539/events","html_url":"https://github.com/mbostock/d3/issues/2539","id":103707624,"number":2539,"title":"Inheritence is nearly impossible right now","user":{"login":"Fathardie","id":4233817,"avatar_url":"https://avatars.githubusercontent.com/u/4233817?v=3","gravatar_id":"","url":"https://api.github.com/users/Fathardie","html_url":"https://github.com/Fathardie","followers_url":"https://api.github.com/users/Fathardie/followers","following_url":"https://api.github.com/users/Fathardie/following{/other_user}","gists_url":"https://api.github.com/users/Fathardie/gists{/gist_id}","starred_url":"https://api.github.com/users/Fathardie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Fathardie/subscriptions","organizations_url":"https://api.github.com/users/Fathardie/orgs","repos_url":"https://api.github.com/users/Fathardie/repos","events_url":"https://api.github.com/users/Fathardie/events{/privacy}","received_events_url":"https://api.github.com/users/Fathardie/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-08-28T12:10:52Z","updated_at":"2015-09-03T10:58:49Z","closed_at":null,"body":"Hi everyone!\r\n\r\nI have been trying to create extensions(inheritence) of certain d3 components but with the current code structure it is really hard to do that. Lets have a look at the axis component to show you an example:\r\n```\r\nfunction axis() {\r\n  function axis(selection) {\r\n\r\n  }\r\n\r\n  axis.scale = function(x) {\r\n    if (!arguments.length) return scale;\r\n    scale = x;\r\n    return axis;\r\n  };\r\n}\r\n```\r\nThis is great and it works! However, when I want to create a function axis_child that inherits everything from axis and extends it with functionality (while creating a new callable function and holding a reference to the old one), we have a problem:\r\n\r\n```\r\nfunction axis_child() {\r\n    var parent = d3.svg.axis();\r\n\r\n   function axis_child(selection) {\r\n       selection.call(axis)\r\n   }\r\n   $.extend(axis_child, parent);\r\n\r\n   axis_child.scale_child = function(y) {\r\n          //Awesome new functionality here\r\n   }\r\n}\r\n```\r\n```\r\nUse case:\r\nselection.call(axis_child().scale().scale_child()) //Error! axis does not have function scale_child\r\n```\r\nThe problem is that the reference inside of axis.scale is to the axis function object and not to this. We can use the scale function from axis_child, but the return value is set to axis therefor rendering any axis_child functions useless thereafter. Also, when we save this instance, we are saving an axis function object and not a axis_child function object!\r\n```\r\nvar axis_child = axis_child().scale()  //Error! This is now an axis, not an axis_child!\r\n```\r\n\r\nWe can fix these issues fairly easily by referencing this instead of the function object but it will take a significant refactoring of all of the \"configuration\" functions for objects such as axis.\r\n\r\nOur inheritence scheme could than be as proposed above. Is it possible to process this request or am I missing something crucial which is the reason you chose the direct function object reference over the this reference?\r\n\r\nThanks!\r\n"},"comment":{"url":"https://api.github.com/repos/mbostock/d3/issues/comments/137407892","html_url":"https://github.com/mbostock/d3/issues/2539#issuecomment-137407892","issue_url":"https://api.github.com/repos/mbostock/d3/issues/2539","id":137407892,"user":{"login":"Fathardie","id":4233817,"avatar_url":"https://avatars.githubusercontent.com/u/4233817?v=3","gravatar_id":"","url":"https://api.github.com/users/Fathardie","html_url":"https://github.com/Fathardie","followers_url":"https://api.github.com/users/Fathardie/followers","following_url":"https://api.github.com/users/Fathardie/following{/other_user}","gists_url":"https://api.github.com/users/Fathardie/gists{/gist_id}","starred_url":"https://api.github.com/users/Fathardie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Fathardie/subscriptions","organizations_url":"https://api.github.com/users/Fathardie/orgs","repos_url":"https://api.github.com/users/Fathardie/repos","events_url":"https://api.github.com/users/Fathardie/events{/privacy}","received_events_url":"https://api.github.com/users/Fathardie/received_events","type":"User","site_admin":false},"created_at":"2015-09-03T10:58:49Z","updated_at":"2015-09-03T10:58:49Z","body":"That indeed has almost the exact functionality which I need! Thank you! However, in order for this to work, a lot of wrapper functions are created. For each of the functions in the source element one wrapper is created and that is added to target. By refactoring to 'return this', wouldn't we be able to remove the wrapper all together and just copy functions straight up? It is a very small performance increase, but a performance increase all together. "}}