I. E. Smith-Heisters
2008-Feb-18 22:47 UTC
inheriting static/class methods with new prototype.js class creation
Hey all. I want something like this to work: var Foo = Class.create(); Foo.myStaticMethod = function(){alert("I''m a static method!")}; Foo.myStaticMethod(); // works var f = new Foo(); f.myStaticMethod(); // doesn''t work, but that''s good--I don''t want it to var Bar = Class.create(Foo); Bar.myStaticMethod(); // doesn''t work, undefined :( I''ve seen a lot of posts on creating private instance methods and class methods that don''t inherit, but I haven''t found anything on this. Thanks in advance. -Ian --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
kangax
2008-Feb-19 02:59 UTC
Re: inheriting static/class methods with new prototype.js class creation
Ian, Object.extend(Bar, Foo) should do the trick. - kangax On Feb 18, 5:47 pm, "I. E. Smith-Heisters" <i...-ZMJO0lPjCCA@public.gmane.org> wrote:> Hey all. > > I want something like this to work: > > var Foo = Class.create(); > Foo.myStaticMethod = function(){alert("I''m a static method!")}; > Foo.myStaticMethod(); // works > var f = new Foo(); > f.myStaticMethod(); // doesn''t work, but that''s good--I don''t want it > to > var Bar = Class.create(Foo); > Bar.myStaticMethod(); // doesn''t work, undefined :( > > I''ve seen a lot of posts on creating private instance methods and > class methods that don''t inherit, but I haven''t found anything on > this. > > Thanks in advance. > -Ian--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
RobG
2008-Feb-19 12:49 UTC
Re: inheriting static/class methods with new prototype.js class creation
On Feb 19, 8:47 am, "I. E. Smith-Heisters" <i...-ZMJO0lPjCCA@public.gmane.org> wrote:> Hey all. > > I want something like this to work: > > var Foo = Class.create(); > Foo.myStaticMethod = function(){alert("I''m a static method!")}; > Foo.myStaticMethod(); // works > var f = new Foo(); > f.myStaticMethod(); // doesn''t work, but that''s good--I don''t want it > toYou added a property to Foo called myStaticMethod. When you use the new keyword, the object returned is constructed by Foo, it is not an "instance" of Foo as you might expect in classic inheritance (although it is still called an instance). When you create f, Foo is not on it''s inheritance chain or in scope, so neither is any property of Foo, including myStaticMethod.> var Bar = Class.create(Foo); > Bar.myStaticMethod(); // doesn''t work, undefined :(In classic prototype inheritance you would do: function Foo() {} Foo.prototype.myStaticMethod = function(){} var bar = new Foo(); bar.myStaticMethod(); // ta da> I''ve seen a lot of posts on creating private instance methods and > class methods that don''t inherit, but I haven''t found anything on > this.Javascript closures (and just about everything you need to know about Identifier Resolution, Execution Contexts and Scope Chains): <URL: http://www.jibbering.com/faq/faq_notes/closures.html > JavaScript Inheritance articles (and other stuff): <URL: http://javascript.crockford.com/ > When you''re done with that, go over the Class.create code in Prototype.js. -- Rob --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ian Smith-Heisters
2008-Feb-19 18:02 UTC
Re: inheriting static/class methods with new prototype.js class creation
It does indeed! Thanks. On 2/18/08, kangax <kangax-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Ian, > > Object.extend(Bar, Foo) should do the trick. > > - kangax > > On Feb 18, 5:47 pm, "I. E. Smith-Heisters" <i...-ZMJO0lPjCCA@public.gmane.org> wrote: > > Hey all. > > > > I want something like this to work: > > > > var Foo = Class.create(); > > Foo.myStaticMethod = function(){alert("I''m a static method!")}; > > Foo.myStaticMethod(); // works > > var f = new Foo(); > > f.myStaticMethod(); // doesn''t work, but that''s good--I don''t want it > > to > > var Bar = Class.create(Foo); > > Bar.myStaticMethod(); // doesn''t work, undefined :( > > > > I''ve seen a lot of posts on creating private instance methods and > > class methods that don''t inherit, but I haven''t found anything on > > this. > > > > Thanks in advance. > > -Ian > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
RobG
2008-Feb-20 06:56 UTC
Re: inheriting static/class methods with new prototype.js class creation
On Feb 20, 4:02 am, "Ian Smith-Heisters" <i...-ZMJO0lPjCCA@public.gmane.org> wrote:> It does indeed! Thanks.Now I''m confused, I thought you were after some kind of inheritance. What kangax''s suggestion effectively does simply assigne a value to a property: var Bar = Class.create(Foo); Bar.myStaticMethod = Foo.myStaticMethod; Is that inheritance? -- Rob --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
kangax
2008-Feb-20 13:05 UTC
Re: inheriting static/class methods with new prototype.js class creation
Rob, This can hardly be called inheritance, indeed, but rather a naive way of somewhat simulating it (manually copying "parent" object methods into the "child"). There''s a patch in a trac [1] which provides a much more robust solution - Class.addClassMethods - it ensures that all subclasses are extended with class (static) methods (when added via addClasMethods). [1] http://dev.rubyonrails.org/ticket/10089 Best, kangax On Feb 20, 1:56 am, RobG <rg...-AFFH1GffN5hPR4JQBCEnsQ@public.gmane.org> wrote:> On Feb 20, 4:02 am, "Ian Smith-Heisters" <i...-ZMJO0lPjCCA@public.gmane.org> wrote: > > > It does indeed! Thanks. > > Now I''m confused, I thought you were after some kind of inheritance. > What kangax''s suggestion effectively does simply assigne a value to a > property: > > var Bar = Class.create(Foo); > Bar.myStaticMethod = Foo.myStaticMethod; > > Is that inheritance? > > -- > Rob--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ian Smith-Heisters
2008-Feb-20 16:32 UTC
Re: inheriting static/class methods with new prototype.js class creation
agreed. But it works without requiring me to do any hackery to Class.create() to do it implicitly. On 2/19/08, RobG <rgqld-AFFH1GffN5hPR4JQBCEnsQ@public.gmane.org> wrote:> > > > On Feb 20, 4:02 am, "Ian Smith-Heisters" <i...-ZMJO0lPjCCA@public.gmane.org> wrote: > > It does indeed! Thanks. > > > Now I''m confused, I thought you were after some kind of inheritance. > What kangax''s suggestion effectively does simply assigne a value to a > property: > > > var Bar = Class.create(Foo); > > Bar.myStaticMethod = Foo.myStaticMethod; > > > Is that inheritance? > > > > -- > Rob > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---