Hello, I can''t make my mind up over some finer points of an API design and would appreciate any feedback on my code. Hopefully this example is clear... var A = Class.create({ initialize : function(obj){ Object.extend(this, obj) } }); var B = { method : function(){ .... } }; var C = { method: function(){ .... } }; var d = new A(B); var e = new A(C); On initialization each instance of class A is extended with methods from a separate object via Object.extend. I''ve chosen not to use Class#addMethods because each instance of can be extended with a different object, and therefore the prototype chain of class A should remain unaffected. So for so good. However I''m looking to use this taking advantage of Prototype''s inheritance features for the extension objects. I''ve currently got something that looks like this... var A = Class.create({ initialize : function(obj){ Object.extend(this, obj.prototype) } }); var B = Class.create({ method : function(){ .... } }) var C = (B, { method: function($super){ $super(); } }; var d = new A(B); var e = new A(C); It works OK but feels a little clunky. To me it would make more sense to do something like this... var A = Class.create({ initialize : function(obj){ Object.extend(this, obj) } }); var B = Class.create({ method : function(){ .... } }) var C = (B, { method: function($super){ $super(); } }; var d = new A(new (B)); var e = new A(new (C)); Anybody got any strong opinions on which approach is best? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
I don''t see how "var d = new A(B); " is "chunkier" than "var e = new A(new (B));" : ) Am I correct that you want to extend instance with methods, and preserve their (methods'') inheritance? - kangax On Apr 23, 7:22 am, Mike Rumble <mike.rum...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello, > > I can''t make my mind up over some finer points of an API design and > would appreciate any feedback on my code. Hopefully this example is > clear... > > var A = Class.create({ > initialize : function(obj){ > Object.extend(this, obj) > } > > }); > > var B = { > method : function(){ > .... > } > > }; > > var C = { > method: function(){ > .... > } > > }; > > var d = new A(B); > var e = new A(C); > > On initialization each instance of class A is extended with methods > from a separate object via Object.extend. I''ve chosen not to use > Class#addMethods because each instance of can be extended with a > different object, and therefore the prototype chain of class A should > remain unaffected. > > So for so good. > > However I''m looking to use this taking advantage of Prototype''s > inheritance features for the extension objects. I''ve currently got > something that looks like this... > > var A = Class.create({ > initialize : function(obj){ > Object.extend(this, obj.prototype) > } > > }); > > var B = Class.create({ > method : function(){ > .... > } > > }) > > var C = (B, { > method: function($super){ > $super(); > } > > }; > > var d = new A(B); > var e = new A(C); > > It works OK but feels a little clunky. To me it would make more sense > to do something like this... > > var A = Class.create({ > initialize : function(obj){ > Object.extend(this, obj) > } > > }); > > var B = Class.create({ > method : function(){ > .... > } > > }) > > var C = (B, { > method: function($super){ > $super(); > } > > }; > > var d = new A(new (B)); > var e = new A(new (C)); > > Anybody got any strong opinions on which approach is best?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
> I don''t see how "var d = new A(B); " is "chunkier" than "var e = new > A(new (B));" : )I guess it''s a matter of taste then. I prefer Object.extend(this, obj) to Object.extend(this, obj.prototype), just wanted to know if there was really any difference in the two different approaches, and for the record both function identically.> Am I correct that you want to extend instance with methods, and > preserve their (methods'') inheritance?Yeah that''s spot on. On Apr 23, 10:53 pm, kangax <kan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Am I correct that you want to extend instance with methods, and > preserve their (methods'') inheritance? > > - kangax > > On Apr 23, 7:22 am, Mike Rumble <mike.rum...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hello, > > > I can''t make my mind up over some finer points of an API design and > > would appreciate any feedback on my code. Hopefully this example is > > clear... > > > var A = Class.create({ > > initialize : function(obj){ > > Object.extend(this, obj) > > } > > > }); > > > var B = { > > method : function(){ > > .... > > } > > > }; > > > var C = { > > method: function(){ > > .... > > } > > > }; > > > var d = new A(B); > > var e = new A(C); > > > On initialization each instance of class A is extended with methods > > from a separate object via Object.extend. I''ve chosen not to use > > Class#addMethods because each instance of can be extended with a > > different object, and therefore the prototype chain of class A should > > remain unaffected. > > > So for so good. > > > However I''m looking to use this taking advantage of Prototype''s > > inheritance features for the extension objects. I''ve currently got > > something that looks like this... > > > var A = Class.create({ > > initialize : function(obj){ > > Object.extend(this, obj.prototype) > > } > > > }); > > > var B = Class.create({ > > method : function(){ > > .... > > } > > > }) > > > var C = (B, { > > method: function($super){ > > $super(); > > } > > > }; > > > var d = new A(B); > > var e = new A(C); > > > It works OK but feels a little clunky. To me it would make more sense > > to do something like this... > > > var A = Class.create({ > > initialize : function(obj){ > > Object.extend(this, obj) > > } > > > }); > > > var B = Class.create({ > > method : function(){ > > .... > > } > > > }) > > > var C = (B, { > > method: function($super){ > > $super(); > > } > > > }; > > > var d = new A(new (B)); > > var e = new A(new (C)); > > > Anybody got any strong opinions on which approach is best?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
I''m not sure I understand how these 2 are identical. Former one is a constructor function (with properties like "prototype", "subclasses", "superclass"), while latter one is a "prototype" object (with all the shared "instance" methods). As far as mixins and inheritance, I would do something like this: var Predator = Class.create({ hunt: function() { return this.name + '' is hunting''; } }); var WaterPredator = Class.create(Predator, { hunt: function($super) { return $super() + '' under water''; } }) var Shark = Class.create({ initialize : function(name) { this.name = name; }, speak: function(msg) { return this.name + '': '' + msg; } }) // Create a temporary class and blend both prototypes in: var MixedIn = Class.create(WaterPredator, Shark.prototype); new MixedIn(''Sharkie'').name; // "Sharkie" new MixedIn(''Sharkie'').speak(''Hello underwater world''); // "Sharkie: Hello underwater world" new MixedIn(''Sharkie'').hunt(); // "Sharkie is hunting under water" Best, kangax On Apr 25, 7:06 am, Mike Rumble <mike.rum...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I don''t see how "var d = new A(B); " is "chunkier" than "var e = new > > A(new (B));" : ) > > I guess it''s a matter of taste then. I prefer Object.extend(this, obj) > to Object.extend(this, obj.prototype), just wanted to know if there > was really any difference in the two different approaches, and for the > record both function identically. > > > Am I correct that you want to extend instance with methods, and > > preserve their (methods'') inheritance? > > Yeah that''s spot on. > > On Apr 23, 10:53 pm, kangax <kan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Am I correct that you want to extend instance with methods, and > > preserve their (methods'') inheritance? > > > - kangax > > > On Apr 23, 7:22 am, Mike Rumble <mike.rum...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Hello, > > > > I can''t make my mind up over some finer points of an API design and > > > would appreciate any feedback on my code. Hopefully this example is > > > clear... > > > > var A = Class.create({ > > > initialize : function(obj){ > > > Object.extend(this, obj) > > > } > > > > }); > > > > var B = { > > > method : function(){ > > > .... > > > } > > > > }; > > > > var C = { > > > method: function(){ > > > .... > > > } > > > > }; > > > > var d = new A(B); > > > var e = new A(C); > > > > On initialization each instance of class A is extended with methods > > > from a separate object via Object.extend. I''ve chosen not to use > > > Class#addMethods because each instance of can be extended with a > > > different object, and therefore the prototype chain of class A should > > > remain unaffected. > > > > So for so good. > > > > However I''m looking to use this taking advantage of Prototype''s > > > inheritance features for the extension objects. I''ve currently got > > > something that looks like this... > > > > var A = Class.create({ > > > initialize : function(obj){ > > > Object.extend(this, obj.prototype) > > > } > > > > }); > > > > var B = Class.create({ > > > method : function(){ > > > .... > > > } > > > > }) > > > > var C = (B, { > > > method: function($super){ > > > $super(); > > > } > > > > }; > > > > var d = new A(B); > > > var e = new A(C); > > > > It works OK but feels a little clunky. To me it would make more sense > > > to do something like this... > > > > var A = Class.create({ > > > initialize : function(obj){ > > > Object.extend(this, obj) > > > } > > > > }); > > > > var B = Class.create({ > > > method : function(){ > > > .... > > > } > > > > }) > > > > var C = (B, { > > > method: function($super){ > > > $super(); > > > } > > > > }; > > > > var d = new A(new (B)); > > > var e = new A(new (C)); > > > > Anybody got any strong opinions on which approach is best?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
> I''m not sure I understand how these 2 are identical. Former one is a > constructor function (with properties like "prototype", "subclasses", > "superclass"), while latter one is a "prototype" object (with all the > shared "instance" methods).I guess my point is not that they are identical, but whether I copy methods to my instance from an instance of another class, or directly from it''s prototype I end up with the same result. Thanks for your code example, it looks like a sensible way of doing things and I''ll be sure to explore it further. On Apr 25, 6:32 pm, kangax <kan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m not sure I understand how these 2 are identical. Former one is a > constructor function (with properties like "prototype", "subclasses", > "superclass"), while latter one is a "prototype" object (with all the > shared "instance" methods). > > As far as mixins and inheritance, I would do something like this: > > var Predator = Class.create({ > hunt: function() { > return this.name + '' is hunting''; > } > > }); > > var WaterPredator = Class.create(Predator, { > hunt: function($super) { > return $super() + '' under water''; > } > > }) > > var Shark = Class.create({ > initialize : function(name) { > this.name = name; > }, > speak: function(msg) { > return this.name + '': '' + msg; > } > > }) > > // Create a temporary class and blend both prototypes in: > var MixedIn = Class.create(WaterPredator, Shark.prototype); > > new MixedIn(''Sharkie'').name; // "Sharkie" > new MixedIn(''Sharkie'').speak(''Hello underwater world''); // "Sharkie: > Hello underwater world" > new MixedIn(''Sharkie'').hunt(); // "Sharkie is hunting under water" > > Best, > kangax > > On Apr 25, 7:06 am, Mike Rumble <mike.rum...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > I don''t see how "var d = new A(B); " is "chunkier" than "var e = new > > > A(new (B));" : ) > > > I guess it''s a matter of taste then. I prefer Object.extend(this, obj) > > to Object.extend(this, obj.prototype), just wanted to know if there > > was really any difference in the two different approaches, and for the > > record both function identically. > > > > Am I correct that you want to extend instance with methods, and > > > preserve their (methods'') inheritance? > > > Yeah that''s spot on. > > > On Apr 23, 10:53 pm, kangax <kan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Am I correct that you want to extend instance with methods, and > > > preserve their (methods'') inheritance? > > > > - kangax > > > > On Apr 23, 7:22 am, Mike Rumble <mike.rum...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Hello, > > > > > I can''t make my mind up over some finer points of an API design and > > > > would appreciate any feedback on my code. Hopefully this example is > > > > clear... > > > > > var A = Class.create({ > > > > initialize : function(obj){ > > > > Object.extend(this, obj) > > > > } > > > > > }); > > > > > var B = { > > > > method : function(){ > > > > .... > > > > } > > > > > }; > > > > > var C = { > > > > method: function(){ > > > > .... > > > > } > > > > > }; > > > > > var d = new A(B); > > > > var e = new A(C); > > > > > On initialization each instance of class A is extended with methods > > > > from a separate object via Object.extend. I''ve chosen not to use > > > > Class#addMethods because each instance of can be extended with a > > > > different object, and therefore the prototype chain of class A should > > > > remain unaffected. > > > > > So for so good. > > > > > However I''m looking to use this taking advantage of Prototype''s > > > > inheritance features for the extension objects. I''ve currently got > > > > something that looks like this... > > > > > var A = Class.create({ > > > > initialize : function(obj){ > > > > Object.extend(this, obj.prototype) > > > > } > > > > > }); > > > > > var B = Class.create({ > > > > method : function(){ > > > > .... > > > > } > > > > > }) > > > > > var C = (B, { > > > > method: function($super){ > > > > $super(); > > > > } > > > > > }; > > > > > var d = new A(B); > > > > var e = new A(C); > > > > > It works OK but feels a little clunky. To me it would make more sense > > > > to do something like this... > > > > > var A = Class.create({ > > > > initialize : function(obj){ > > > > Object.extend(this, obj) > > > > } > > > > > }); > > > > > var B = Class.create({ > > > > method : function(){ > > > > .... > > > > } > > > > > }) > > > > > var C = (B, { > > > > method: function($super){ > > > > $super(); > > > > } > > > > > }; > > > > > var d = new A(new (B)); > > > > var e = new A(new (C)); > > > > > Anybody got any strong opinions on which approach is best?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---