es-discuss Digest, Vol 56, Issue 57

John J Barton johnjbarton at johnjbarton.com
Fri Oct 7 08:52:40 PDT 2011


On Fri, Oct 7, 2011 at 8:34 AM, Axel Rauschmayer <axel at rauschma.de> wrote:

> *From: *John J Barton <johnjbarton at johnjbarton.com>
> *Subject: **Re: Object.extends (was Re: traits feedback)*
> *Date: *October 7, 2011 16:49:50 GMT+02:00
> *To: *Quildreen Motta <quildreen at gmail.com>
> *Cc: *John-David Dalton <john.david.dalton at gmail.com>, es-discuss Steen <
> es-discuss at mozilla.org>
>
> Several people advocate Object.extend() that copies only own properties. I
> don't understand why; I'll make the case for copying all properties.
>
> At a call site I use, say
>    foo.bar();
> Ordinarily I should not be concerned about details of bar()'s
> implementation. In particular I should not be concerned if bar() is own or
> not.
>
> Now I decide to create a new object from |foo|, by adding properties with
> Object.extend():
>   var fuz = Object.extend(foo, {paper:'in', shoes:'my'});
> later I write
>   fuz.bar();
>
> If Object.extend() only uses own properties, the success or failure of that
> statement depends upon the location of bar() in foo.  While one might argue
> that use of extend() is one case where devs should be concerned with details
> of bar()'s implementation, how does it help in this case? If bar() is in a
> prototype then I am back to manual property manipulation get the |fuz|
> object I want.
>
> If Object.extend() copies all properties (into prototype or not is a
> different issue) then the resulting object acts as an extension of the one I
> want to extend.
>
> On the other side I don't see any advantage to copying only own properties.
> Again I will suggest Object.getOwnProperties() as a way of providing the
> answer some would like to see.  Again I will point out that Object.extend()
> with plain objects is not affected by these issues.
>
>
> If you do something like
>      var fuz = Object.extend(foo, {paper:'in', shoes:'my'});
>
> Then fuz will get all properties of Object.prototype, again, as duplicates.
> In the above, you are clearly most interested in what you see in the literal
> and those are the own properties.
>

I don't understand how you can get properties as duplicates in JS.
I disagree, since in my example I specifically point out that fuz.bar()
should work.


>
> Also note that JavaScript only changes properties in the first object in a
> prototype chain:
>     var proto = { foo: 3 };
>     var obj = Object.create(proto);
>     obj.foo = 5;
>     console.log(proto.foo); // 3
>

Fine, but not germane as far as I can tell.

>
> And this kind of "extend" enables poor man’s cloning as follows:
>     var orig = { foo: "abc" };
>     var clone = Object.extend(Object.create(Object.getPrototypeOf(orig)),
> orig);
>
> Sadly this code will fail since -- surprise! -- Object.create() does not
take an object as the second argument.


> I would prefer the name Object.copyOwnPropertiesTo(source, target) or
> Object.copyOwnTo(source, target) to the name “extend” (which, to me,
> suggests inheritance).
>

Since we live in a right to left world (a = b();) we need the target on the
left. Then multiple RHS also works easily. However, note that if
Object.create() were fixed:
   var clone = Object.create(Object.getPrototypeOf(orig),
Object.getOwnProperties(orig));

jjb
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20111007/2b6a9ac7/attachment-0001.html>


More information about the es-discuss mailing list