{"comment":{"created_at":"2011-10-10T22:15:55Z","body":"@michaelficarra and @maccman Sorry missed your comment while writing my last one. That is a good point, I think I have a solution though... give me 15 to whip it up.","updated_at":"2011-10-10T22:15:55Z","url":"https://api.github.com/repos/jashkenas/coffee-script/issues/comments/2353038","id":2353038,"user":{"avatar_url":"https://secure.gravatar.com/avatar/063a6feef730cf0d6c8eb36eb23bc86d?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","url":"https://api.github.com/users/soniciq","id":123268,"login":"soniciq"}},"action":"created","issue":{"number":1762,"created_at":"2011-10-10T18:34:45Z","pull_request":{"diff_url":null,"patch_url":null,"html_url":null},"body":"Hi,\r\n\r\nThe one thing keeping me from using CoffeeScript full-time is that it's class system cannot be extended in any way. Below is an idea that allows users to hook into the class system, it's very similar to that in Ruby and Spine.js.\r\n\r\nI am suggesting that the following coffee script:\r\n\r\n```coffeescript\r\nclass Bar\r\n\r\nclass Foo extends Bar\r\n  myInstanceMethod: -> console.log 'I am an instance method';\r\n  @myClassMethod: -> console.log 'I am a class method';\r\n```\r\n\r\nShould generate:\r\n\r\n```javascript\r\nvar Bar, Foo;\r\nvar __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {\r\n  var mixin = function(target, obj) {\r\n    for (var key in obj) { if (__hasProp.call(obj, key)) target[key] = obj[key]; } };\r\n  mixin(child, parent);  \r\n  function ctor() { this.constructor = child; }\r\n  ctor.prototype = parent.prototype;\r\n  child.prototype = new ctor;\r\n  child.__super__ = parent.prototype;\r\n  child.include = child.include || function(obj) { mixin(this.prototype, obj); };\r\n  child.extend = child.extend || function(obj) { mixin(this, obj); };\r\n  if (parent.inherited) parent.inherited(child);\r\n  return child;\r\n};\r\nBar = (function() {\r\n  function Bar() {}\r\n  return Bar;\r\n})();\r\nFoo = (function() {\r\n  __extends(Foo, Bar);\r\n  function Foo() {\r\n    Foo.__super__.constructor.apply(this, arguments);\r\n  }\r\n  Foo.include({\r\n    myInstanceMethod: function() {\r\n      return console.log('I am an instance method');\r\n    }\r\n  });\r\n  Foo.extend({\r\n    myClassMethod: function() {\r\n      return console.log('I am a class method');\r\n    }\r\n  });\r\n  return Foo;\r\n})();\r\n```\r\n\r\nThis way, should Bar want to hook into the class system it could do the following:\r\n\r\n```coffeescript\r\nclass Bar\r\n  @include: (obj) ->\r\n    console.log \"About to declare: '#{key}' on proto\" for key in obj\r\n    super\r\n\r\n  @extend: (obj) ->\r\n    console.log \"About to declare: '#{key}' on class\" for key in obj\r\n    super  \r\n\r\nclass Foo extends Bar\r\n  myInstanceMethod: -> console.log 'I am an instance method';\r\n  @myClassMethod: -> console.log 'I am a class method';\r\n```\r\n\r\nI think this would make CoffeeScript's class system truly powerful and allow things like Ext's preprocessors... the applications are endless. The other benefit of this is that the resulting minified JS would be smaller due to the object notation used in `include` and `extend`. Obviously the `include` and `extend` function names are just a suggestion but I think the likeness to Ruby is a good thing. To avoid conflicts with existing code, they could always be `__include` and `__exclude`.\r\n\r\nThis tiny change also introduces Ruby-esque 'Modules' e.g.\r\n\r\n```coffeescript\r\nCommonInstanceMethods =\r\n  sharedFunction -> console.log 'I come from CommonInstanceMethods'\r\n\r\nCommonClassMethods =\r\n  sharedFunction -> console.log 'I come from CommonClassMethods'\r\n\r\nclass Foo\r\n  @include CommonInstanceMethods\r\n  @extend CommonClassMethods\r\n\r\nclass Bar\r\n  @include CommonInstanceMethods\r\n  @extend CommonClassMethods\r\n```\r\n\r\nI would create a patch but don't know enough about parsers to be able to implement it. I really hope we can add this as currently there is no way to extend the class system.\r\n\r\n\r\nThanks,\r\n\r\nJamie","comments":12,"title":"Class system extendability - A simple solution","updated_at":"2011-10-10T22:15:55Z","url":"https://api.github.com/repos/jashkenas/coffee-script/issues/1762","id":1866750,"assignee":null,"milestone":null,"closed_at":null,"user":{"avatar_url":"https://secure.gravatar.com/avatar/063a6feef730cf0d6c8eb36eb23bc86d?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","url":"https://api.github.com/users/soniciq","id":123268,"login":"soniciq"},"html_url":"https://github.com/jashkenas/coffee-script/issues/1762","labels":[],"state":"open"},"legacy":{"issue_id":1866750,"comment_id":2353038}}