> 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__' ]


> 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).


> 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.

>  
> 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(). But, in a way, the proto operator <| is that fix. However, 
your are on to something with the above which will work as follows in ES.next:
   var clone = Object.create(Object.getPrototypeOf(orig), 
Object.getOwnPropertyDescriptors(orig));

-- 
Dr. Axel Rauschmayer

a...@rauschma.de
twitter.com/rauschma

home: rauschma.de
blog: 2ality.com



_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to