Hi guys, I have a question about the implementation of the Object.extend (<v1.6). Can you (very) briefly discuss the tradeoffs between the following two approaches and the motivation for choosing the first one for the framework. Thank you. Regards, GC =========== Approach 1 (current) ==== Object.extend = function (a, b) { for (var c in b) { a[c] = b[c]; } return a; } --------------------------------------------------------------- =========== Approach 2 ========== Object.extend = function (a, b) { var E = function() {}; E.prototype = b.prototype; a.prototype = new E(); a.prototype.constructor = a; return a; } --------------------------------------------------------------- --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Tobie Langel
2008-May-03 12:41 UTC
Re: Prototype: Why was [Object.extend] implemented that way?
Hi, Sure, in approach 2, you can no longer delete the properties. (Not sure about your syntax for that second approach, btw). Best, Tobie On May 3, 12:48 pm, gcalm <GeorgeC...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi guys, > > I have a question about the implementation of the Object.extend (<> v1.6). > > Can you (very) briefly discuss the tradeoffs between the following two > approaches and the motivation for choosing the first one for the > framework. > > Thank you. > > Regards, > GC > > =========== Approach 1 (current) ====> > Object.extend = function (a, b) { > for (var c in b) { > a[c] = b[c]; > } > return a; > > } > > --------------------------------------------------------------- > > =========== Approach 2 ==========> > Object.extend = function (a, b) { > var E = function() {}; > E.prototype = b.prototype; > a.prototype = new E(); > a.prototype.constructor = a; > return a; > > } > > -----------------------------------------------------------------~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
GC, both of these are used in prototype. "Object.extend" is a simple way to "manually" (using for/in) copy properties of one object into another. It''s useful for object cloning, setting up default "options" in "classes", or just creating/updating object properties in a batch. Second approach, (first introduced by Crockford, I believe) is used in Class.addMethods (which is in turn used by Class.create) for setting up proper "class" inheritance. Using Object.extend for "subclassing" would break things (e.g. instanceof operator) and be quite inefficient (as instead of sharing properties via prototype chain, objects would "carry" them directly). Best, kangax On May 3, 6:48 am, gcalm <GeorgeC...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi guys, > > I have a question about the implementation of the Object.extend (<> v1.6). > > Can you (very) briefly discuss the tradeoffs between the following two > approaches and the motivation for choosing the first one for the > framework. > > Thank you. > > Regards, > GC > > =========== Approach 1 (current) ====> > Object.extend = function (a, b) { > for (var c in b) { > a[c] = b[c]; > } > return a; > > } > > --------------------------------------------------------------- > > =========== Approach 2 ==========> > Object.extend = function (a, b) { > var E = function() {}; > E.prototype = b.prototype; > a.prototype = new E(); > a.prototype.constructor = a; > return a; > > } > > -----------------------------------------------------------------~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---