Hello I´m making a class using prototype''s class.create(), like this: var onewClass = Class.create(); onewClass.prototype = { initialize : function(array) { this.variable = "fooo"; this.array = array; }, function1 : function() { this.array.each(function(element){ alert(element); alert(this.variable); }); } }; and I intance it like this: var array = ("house", "car", "tree"); var newClass = new onewClass(array); newClass.function1(); The problem is that the alerts appear, but this.variable says "undefined", how can i do to make it say "fooo", as is declarated in the initialize part? Thank you and sorry for my english :) :) !! -- Daniel Herrero Dávila _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
this.array.each((function(element){ alert(element); alert(this.variable); }).bind(this)); Hope this helps, Nicolas
Thank you Nicolas!! your soluction worked wonderfull. Now I have another cuestion related to this: I have a class with an object inside: var onewClass = Class.create(); onewClass.prototype = { initialize : function() { this.variable = "fooo"; }, myrules : {"fooo" : this.variable } }; and I intance it like this: var newClass = new onewClass(); if I do alert(newClass.myrules.fooo) it doesn´t show "fooo". How can I do to correct this? Thank you!! -- Daniel Herrero Dávila _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
> var onewClass = Class.create(); > onewClass.prototype = { > initialize : function() { > this.variable = "fooo"; > }, > myrules : {"fooo" : this.variable > } > };This may have just been a typo by you, but I think you just need to remove the quotes from the "fooo" in your object... var onewClass = Class.create(); onewClass.prototype = { initialize : function() { this.variable = "fooo"; }, myrules : {fooo: this.variable } }; The information transmitted in this electronic mail is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
On 3/27/06, Daniel Herrero <herrerodani-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Now I have another cuestion related to this: I have a class with an object > inside: > > var onewClass = Class.create(); > onewClass.prototype = { > initialize : function() { > this.variable = "fooo"; > }, > myrules : {"fooo" : this.variable > > } > }; > and I intance it like this: > var newClass = new onewClass(); > if I do alert(newClass.myrules.fooo) it doesn´t show "fooo". How can I do > to correct this? Thank you!!fooo is being set at "compile time" to this.variable which isn''t defined until you call new onewClass(). Just define this.myrules in initialize() and you should be able to access them via newClass.myrules like in your example. Ryan, the quotes around fooo are valid syntax. Todd
> Ryan, the quotes around fooo are valid syntax.Ahh, oops, never knew that. Thanks. The information transmitted in this electronic mail is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers.