Object.define ==> Object.mixin??

Andrea Giammarchi andrea.giammarchi at gmail.com
Wed Dec 12 09:08:15 PST 2012


agreed, and actually one of the best case I've seen in a while for
Array#reduce.

Can I write this as it should be for those who'll read one day later on and
making it also static ?

"mixin" in Object || Object.defineProperty(Object, "mixin", {
  value: function mixin(target, source) {
    return Object.getOwnPropertyNames(source).reduce(function (target,
name) {
      return Object.defineProperty(
        target, name, Object.getOwnPropertyDescriptor(source, name)
      );
    }, target);
  }
});

also thinking about try/catch to avoid mixin overwrite ... I believe it's
OK if that throws an error but then it should throw regardless if the
target object had already that property defined ?

br


On Wed, Dec 12, 2012 at 7:43 AM, Brandon Benvie
<brandon at brandonbenvie.com>wrote:

> Works well as an arrow function due to not needing to turn it into a block
> body, although forEach and a comma would work too.
>
> Object.mixin = (target, source) =>
>   Object.keys(source).reduce((target, key) =>
>     Object.defineProperty(target, key,
> Object.getOwnPropertyDescriptor(source, key))
>    ), target);
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20121212/b0de51f6/attachment.html>


More information about the es-discuss mailing list