On Fri, Oct 7, 2011 at 9:23 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.
>
>
> Given the following:
> var target = Object.create(null); // target has not prototype
> console.log(toString in target); // false
> var source = { foo: 123 };
> console.log(toString in source); // true
>
> Object.extend(target, source);
> console.log(toString in target); // true
>
> Object.extend would also add all of the following properties to target
> (copied from Object.prototype, which is the prototype of each object
> literal).
>
> > Object.getOwnPropertyNames(Object.getPrototypeOf({}))
> [ 'toString',
>   '__lookupGetter__',
>   '__defineGetter__',
>   'toLocaleString',
>   'hasOwnProperty',
>   'valueOf',
>   '__defineSetter__',
>   'constructor',
>   'propertyIsEnumerable',
>   'isPrototypeOf',
>   '__lookupSetter__' ]
>

Sorry to be dense, but I still don't get it. How can an object have
duplicate properties? I understand that own properties override properties
on prototypes.


> 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.
>
>
> Loosely, it shows that own properties have always had special status in
> JavaScript (prototypes are there specifically to be shared).
>

Note this specialness is about writing, not reading.
I'm fine with own properties having special status; I'm fine with
Object.extend() treating own properties special. I'm not fine with
Object.extend() that produces an object without some of the properties of
the input arguments.


>
> 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() only has a single argument above, it creates an empty
> object.
>

Yes, sorry. However if Object.create took a simple object in the second
argument then you don't need to repurpose extend() to create an object from
a prototype and some properties, Object.create() can do it.

>
>
>
>> 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.
>
>
> Swapping the parameters would is fine with me, then the name should
> probably be changed to something like copyOwnFrom(target, source).
>
> However, note that if Object.create() were fixed:
>    var clone = Object.create(Object.getPrototypeOf(orig),
> Object.getOwnProperties(orig));
>
>
> I don’t see a simple way of “fixing” (property descriptors do have their
> uses) Object.create().
>

Just allow the second argument to be property descriptor *or* object.


> But, in a way, the proto operator <| is that fix.
>

Well as far as I can make out from
http://wiki.ecmascript.org/doku.php?id=harmony:proto_operator that operator
is limited to member expressions.

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

Reply via email to