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 <[EMAIL PROTECTED]> 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 [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to