Object.assign and __proto__ property

Rick Waldron waldron.rick at gmail.com
Mon Oct 21 20:37:44 PDT 2013


On Mon, Oct 21, 2013 at 11:26 PM, Andrea Giammarchi <
andrea.giammarchi at gmail.com> wrote:

> As sanity/integrity check, `Object.assign` should be something like this
> to me ...
>
> ```javascript
>
> for (var property in source) {
>   if (Object.prototype.hasOwnProperty.call(source, key)) {
>     if (key in target) {
>       Object.defineProperty(
>         target, key,
>         Object.getOwnPropertyDescriptor(
>           source, key
>         )
>       );
>     } else {
>       target[key] = source[key];
>     }
>

This is exactly why there is Object.assign and Object.mixin (originally
called Object.define), because you can't make this semantic decision and
assume that it's what all code wants all the time.



>   }
> }
>
> ```
>

Traceur has:

function assign(target, source) {
  var props = $getOwnPropertyNames(source);
  var p, length = props.length;
  for (p = 0; p < length; p++) {
    target[props[p]] = source[props[p]];
  }
  return target;
}

Which is correct.





> If this is what every dev should do in order to have a meaningful version
> of current `Object.assign`,
>

But it's wrong.



> I agree with Rick that in core this could/would be properly optimized and
> made faster.
>

Thanks

Rick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20131021/d02247e6/attachment.html>


More information about the es-discuss mailing list