Hi, I''ve got a little problem. I''d like to assign an instance method to an instance property inside a class created via prototype.js. Below there''s an example that doesn''t work. I''ve made a lot of tests, but I cannot figure out the problem. Any idea? ---------------- var myClass = Class.create(); myClass.prototype = { initialize:function(arg1,arg2){ this.prop1 = arg1; this.prop2 = arg2; //assign method1 to methodProp this.methodProp = method1; //if I alert this.methodProp here, I have the function code but...*** }, method1:function(){ [code..] }, method2:function(){ //use method 1 via this.methodProp //*** If I alert this.methodProp here, I get undefined.... this.methodProp(); } } ---------------- --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Keith Pitt
2007-May-02 08:53 UTC
Re: prototype based classes: assigning methods to properties?
Gday Luca, Maybe try this instead? initialize:function(arg1,arg2){ this.prop1 = arg1; this.prop2 = arg2; //assign method1 to methodProp this.methodProp = this.method1; }, Ive added "this" infront of the assignment of this.methodProp. Keith Luca wrote:> Hi, I''ve got a little problem. I''d like to assign an instance method > to an instance property inside a class created via prototype.js. Below > there''s an example that doesn''t work. I''ve made a lot of tests, but I > cannot figure out the problem. Any idea? > > ---------------- > > var myClass = Class.create(); > > myClass.prototype = { > initialize:function(arg1,arg2){ > this.prop1 = arg1; > this.prop2 = arg2; > > //assign method1 to methodProp > this.methodProp = method1; > //if I alert this.methodProp here, I have the function > code but...*** > }, > > method1:function(){ > [code..] > }, > > method2:function(){ > //use method 1 via this.methodProp > //*** If I alert this.methodProp here, I get > undefined.... > this.methodProp(); > } > } > > ---------------- > > > > >-- MindVision Interactive Ph: (08) 8212 9544 Fax: (08) 8212 9644 E-Mail: keith-ildoxR50Kj+k+/BmZukYBiV4CK0nxESZKQEueVp/e6I@public.gmane.org Web: www.mindvision.com.au --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Yes of course, it was a stupid syntactic error... :P Thank you very much... On 2 Mag, 10:53, Keith Pitt <k...-ve2Fw/1u49m1JilXf3s4iIdd74u8MsAO@public.gmane.org> wrote:> Gday Luca, > > Maybe try this instead? > > initialize:function(arg1,arg2){ > this.prop1 = arg1; > this.prop2 = arg2; > > //assign method1 to methodProp > this.methodProp = this.method1; > > }, > > Ive added "this" infront of the assignment of this.methodProp. > > Keith > > > > Luca wrote: > > Hi, I''ve got a little problem. I''d like to assign an instance method > > to an instance property inside a class created via prototype.js. Below > > there''s an example that doesn''t work. I''ve made a lot of tests, but I > > cannot figure out the problem. Any idea? > > > ---------------- > > > var myClass = Class.create(); > > > myClass.prototype = { > > initialize:function(arg1,arg2){ > > this.prop1 = arg1; > > this.prop2 = arg2; > > > //assign method1 to methodProp > > this.methodProp = method1; > > //if I alert this.methodProp here, I have the function > > code but...*** > > }, > > > method1:function(){ > > [code..] > > }, > > > method2:function(){ > > //use method 1 via this.methodProp > > //*** If I alert this.methodProp here, I get > > undefined.... > > this.methodProp(); > > } > > } > > > ---------------- > > -- > MindVision Interactive > > Ph: (08) 8212 9544 > Fax: (08) 8212 9644 > > E-Mail: k...-ildoxR50Kj+k+/BmZukYBiV4CK0nxESZKQEueVp/e6I@public.gmane.org > Web:www.mindvision.com.au--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---