fabtafelmak-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Aug-16 11:03 UTC
Prototype : "this" scope in object
Hi all, I just would like to know if it''s possible to change the scope of "this" keyword inside an object, like that : var Obj = Class.create(); Obj.prototype = { plugin : { getProp : function () { // here "this" keyword should point to the parent object return this.prop; } }, initialize : function () { this.prop = ''test''; } } var o = new Obj(); alert(o.plugin.getProp()); Is there a way to do that or do I have to find an other way ? thanks rekam --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Aug 16, 9:03 pm, "fabtafel...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <fabtafel...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi all, > > I just would like to know if it''s possible to change the scope of > "this" keyword inside an object, like that :The this keyword doesn''t have a scope, it has a value that is set by the call.> var Obj = Class.create(); > Obj.prototype = { > > plugin : { > getProp : function () { > // here "this" keyword should point to the parent object > return this.prop; > } > }, > > initialize : function () { > this.prop = ''test''; > } > } > > var o = new Obj(); > alert(o.plugin.getProp()); > > Is there a way to do that or do I have to find an other way ?Here is one way: function Obj() { } Obj.prototype.plugin = { getProp:function() { return this.prop; }, initialise: function() { this.prop = ''test''; } } var o = new Obj(); o.plugin.initialise(); alert(o.plugin.getProp()); -- 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 -~----------~----~----~----~------~----~------~--~---
Great question and great answer. Cheers, ~tim On Aug 16, 10:30 am, RobG <rg...-AFFH1GffN5hPR4JQBCEnsQ@public.gmane.org> wrote:> On Aug 16, 9:03 pm, "fabtafel...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <fabtafel...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > Hi all, > > > I just would like to know if it''s possible to change the scope of > > "this" keyword inside an object, like that : > > The this keyword doesn''t have a scope, it has a value that is set by > the call. > > > > > var Obj = Class.create(); > > Obj.prototype = { > > > plugin : { > > getProp : function () { > > // here "this" keyword should point to the parent object > > return this.prop; > > } > > }, > > > initialize : function () { > > this.prop = ''test''; > > } > > } > > > var o = new Obj(); > > alert(o.plugin.getProp()); > > > Is there a way to do that or do I have to find an other way ? > > Here is one way: > > function Obj() { > > } > > Obj.prototype.plugin = { > getProp:function() { > return this.prop; > }, > initialise: function() { > this.prop = ''test''; > } > > } > > var o = new Obj(); > o.plugin.initialise(); > alert(o.plugin.getProp()); > > -- > 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 -~----------~----~----~----~------~----~------~--~---
Prototype documentation states: http://www.prototypejs.org/api/function/bind In JavaScript, functions are executed in a specific context (often referred to as "scope"). Inside the function the this keyword becomes a reference to that scope. I think this is what you want: var Obj = Class.create(); Obj.prototype = { plugin : {}, initialize : function () { this.prop = ''test''; this.plugin.getProp = function(){ //here "this" keyword should point to the parent object return this.prop; }.bind(this); } }; var o = new Obj(); alert(o.plugin.getProp()); --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Aug 18, 4:37 am, jdalton <jdalton...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Prototype documentation states:http://www.prototypejs.org/api/function/bind > > In JavaScript, functions are executed in a specific context (often > referred to as "scope"). > Inside the function the this keyword becomes a reference to that > scope. > > I think this is what you want: > > var Obj = Class.create(); > Obj.prototype = { > > plugin : {}, > > initialize : function () { > this.prop = ''test'';I think the OP intended prop to be a property of plugin, not the new object itself (though I might be wrong about that).> this.plugin.getProp = function(){ > //here "this" keyword should point to the parent object > return this.prop; > }.bind(this);That has a very different result to what I posted - it creates a prop property on the new object, then uses bind (which is a prototype wrapper for apply) to create a closure back to it from the anonymous getProp function within the plugin object. The code I supplied creates the property on the plugin object and getProp returns that property.> } > }; > > var o = new Obj(); > alert(o.plugin.getProp());alert(o.prop); // ''test'' Did the OP want o.prop, or o.plugin.prop? -- 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 -~----------~----~----~----~------~----~------~--~---