mcrawford-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Dec-04 00:58 UTC
Am I supposed to be using Object.extend() like this?
This is sort of a "I''m just learning and maybe don''t know what I''m doing" question, but I''d like to learn some best practices. When I read about Object.extend, I found myself using it everywhere, but when I read the documentation, it sounds like it was intended to be used in a more specific situation (simulating inheritance) than I keep using it for (just adding properties to an object). An example of how I''m using it: I want to add another pseudo-event to the list of events in Ajax.InPlaceEditor.Listeners, so I''m doing it like so. Object.extend(Ajax.InPlaceEditor.Listeners, {''editor:restart'': ''enterEditMode''}); Is there an easier way to do this that I''m plain missing? Or another example, pseudo-code this time, var baseOptions = { ...some base options for a function call...}; myFunction(baseOptions); // now need to call function with an additional option myFunction(Object.extend({ anotherOption:true }, baseOptions)); Thanks for any pointers or tips. --~--~---------~--~----~------------~-------~--~----~ 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 would say that Object.extend represents composition more than inheritance - it simply copies methods/properties from one object into another one. You seem to be using it just the right way. For inheritance examples see prototype''s official article http://www.prototypejs.org/learn/class-inheritance --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
mcrawford-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> This is sort of a "I''m just learning and maybe don''t know what I''m > doing" question, but I''d like to learn some best practices. > > When I read about Object.extend, I found myself using it everywhere, > but when I read the documentation, it sounds like it was intended to > be used in a more specific situation (simulating inheritance) than I > keep using it for (just adding properties to an object). > > An example of how I''m using it: I want to add another pseudo-event to > the list of events in Ajax.InPlaceEditor.Listeners, so I''m doing it > like so. > > Object.extend(Ajax.InPlaceEditor.Listeners, {''editor:restart'': > ''enterEditMode''}); > > Is there an easier way to do this that I''m plain missing?The standard javascirpt would be: Ajax.InPlaceEditor.Listeners[''editor:restart''] = ''enterEditMode'';> > Or another example, pseudo-code this time, > var baseOptions = { ...some base options for a function call...}; > myFunction(baseOptions); > // now need to call function with an additional option > myFunction(Object.extend({ anotherOption:true }, baseOptions));baseOptions.anotherOption = true; myFunction(baseOptions); -- 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 -~----------~----~----~----~------~----~------~--~---
RobG, on your last line there, you''re overwriting baseOptions.anotherOptionno matter what, whereas the original snippet uses "true" as the default value _if_ baseOptions.anotherOptions is not already set... quite a difference. OP, aside from the fact that your first example, as RobG pointed out, is a bit unnecesary (but I assume you know that and the example was contrived), you''re using the method just as it was intended. On 12/3/07, RobG <rgqld-AFFH1GffN5hPR4JQBCEnsQ@public.gmane.org> wrote:> > > > > mcrawford-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: > > This is sort of a "I''m just learning and maybe don''t know what I''m > > doing" question, but I''d like to learn some best practices. > > > > When I read about Object.extend, I found myself using it everywhere, > > but when I read the documentation, it sounds like it was intended to > > be used in a more specific situation (simulating inheritance) than I > > keep using it for (just adding properties to an object). > > > > An example of how I''m using it: I want to add another pseudo-event to > > the list of events in Ajax.InPlaceEditor.Listeners, so I''m doing it > > like so. > > > > Object.extend(Ajax.InPlaceEditor.Listeners, {''editor:restart'': > > ''enterEditMode''}); > > > > Is there an easier way to do this that I''m plain missing? > > The standard javascirpt would be: > > Ajax.InPlaceEditor.Listeners[''editor:restart''] = ''enterEditMode''; > > > > > > Or another example, pseudo-code this time, > > var baseOptions = { ...some base options for a function call...}; > > myFunction(baseOptions); > > // now need to call function with an additional option > > myFunction(Object.extend({ anotherOption:true }, baseOptions)); > > baseOptions.anotherOption = true; > myFunction(baseOptions); > > > -- > Rob > > >-- Ryan Gahl Manager, Senior Software Engineer Nth Penguin, LLC http://www.nthpenguin.com -- WebWidgetry.com / MashupStudio.com Future Home of the World''s First Complete Web Platform -- Inquire: 1-920-574-2218 Blog: http://www.someElement.com LinkedIn Profile: http://www.linkedin.com/in/ryangahl --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
mcrawford-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Dec-04 16:48 UTC
Re: Am I supposed to be using Object.extend() like this?
Thanks for the answers. I actually brain-blanked on the first example, which I embarrassingly did copy from my code, even though I did know (and realized after posting) that it was a dumb usage. I understand the difference between the two ways of doing it in the second example after checking the .extend() code, that the source object''s properties take precedence. Thanks again. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---