On Fri, Oct 7, 2011 at 9:16 AM, Quildreen Motta <[email protected]> wrote:

> 2011/10/7 John J Barton <[email protected]>
>
>>
>>
>> On Fri, Oct 7, 2011 at 8:34 AM, Axel Rauschmayer <[email protected]>wrote:
>>
>>> 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.
>>
>
> You can't get duplicate properties, because keys are unique. However,
> you'll still get loads of clutter in the target object. Also, again, I'm not
> sure the majority of the use-cases would be concerned with copying the
> properties in the whole prototype chain -- that's expensive! -- and you
> could achieve the latter easily by recursively extending the object.
>

Again I'll ask you to consider that fuz.bar() should work. That is what
extend means.  How can we solve this?

We don't need to flatten the prototype chain, we can extend it.
Object.extend(a,b) can return an object whose own properties are the own
properties of |b| on those of |a| and the prototype chain of |b| appended to
the prototype chain of |a|, dropping duplicate prototypes.

The overhead of these kinds of operations is insignificant, we are
constructing objects not using them. Flattening the prototype chain would
trade more memory for faster property access. Either choice will be wrong
from some cases, but this is a second priority issue.


>
>
>
>> 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.
>
> Object.create here is only taking a single parameter though. `orig' is
> being passed to Object.extend. The kind of problems that arise when you have
> overtly verbose qualified names that you can't even keep track of what's
> where.
>

Perhaps, but this case I just wanted Axel to be wrong in the same way I had
been wrong, so I didn't read his code carefully.

jjb
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to