Object.define ==> Object.mixin??

Herby Vojčík herby at mailbox.sk
Tue Dec 11 10:12:25 PST 2012



Rick Waldron wrote:
>
>
>
> On Tue, Dec 11, 2012 at 12:28 PM, Allen Wirfs-Brock
> <allen at wirfs-brock.com <mailto:allen at wirfs-brock.com>> wrote:
>
>     It also made me think that perhaps Object.mixin might be a more
>     intuitive name for such a function.
>
>
> This name is certainly more real-word-friendly.
>
> The example code that follows "A pure ECMAScript 5 version of mixin()
> would be:" is basically what I imagined Object.define would be, but with
> a slight modification in that Object.assign returns the target object,
> so should Object.mixin:

Yes.

> Object.mixin = function(receiver, supplier) {
>    return Object.keys(supplier).reduce(function(receiver, property) {
>      return Object.defineProperty(
>        receiver, property, Object.getOwnPropertyDescriptor(supplier,
> property)
>      );
>    }, receiver);
> };

I know this may look like a nitpick, but, why such clever code? I had 
the problem reading it (I just skipped it); I see no "reducing" process 
here. Maybe I am just not functional enough. I wouldn't use inject:into: 
when doing this in smalltalk, but do:.

What's wrong with forEach followed by return receiver?

> Rick

Herby


More information about the es-discuss mailing list