Object.mixin( source, target ) | arguments changed

Peter Seliger peter.seliger at googlemail.com
Thu Jun 13 16:18:29 PDT 2013

If one thinks about how Andrea Giammarchi might have come up
with his already reliable implementation of [Object.mixin] ...


... the need for changing the order of arguments most probably
will become less desirable.

It combines both approaches that today's developers rely on
if they try to "extend" or "augment" objects. If it was just
for the many implementations of [*.extend] that do stepwise
copy properties from one object to another it wasn't even worth
discussing it. The second approach mostly gets referred to as
"functional or function based mixins" - and Andrea's implementation
of [Object.mixin] ...

  function (
    target, // object to enrich with
    source, // mixin object or Trait (Function)
    args    // optional arguments for Trait
  ) { ... }

... considers the different variants of this composition
pattern by providing/allowing a third parameter for what
I'm willing to call a "Privileged Trait" or a "Privileged Mixin".

    PrivilegedTrait = function (injectedReadOnlyState) {
      // implementation

    obj = {
      // description
  PrivilegedTrait.call(obj, {state:"readonly"});

//source.apply(target, args);

Looking at the last line of the given example above there
was really no other solution than Andrea came up with in
order to support in a future proof way what already exists
today in various ways - though neither amount nor precedence
of this methods arguments should be changed.

Secondly. What are the advantages of invoking [Object.mixin]
with more than one source over the disadvantage of loosing
this methods clean interface if there is [Array.forEach] ...

[source_A, source_B, source_C].forEach(function (source) {
  Object.mixin(target, source);

Thirdly. Andrea's approach easily can be "shimmed" for ES3
and does not hide JavaScript's natural/native predisposition
for Trait/Mixin composition under to much syntactic sugar.

Last. Only if there should ever the naming of "mixin"
seriously be questioned again I would propose to choose
[Object.compose] as an alternative.

additional links that might proof what I just brought into discussion:

thanks - Peter

On Thu, Jun 13, 2013 at 5:38 PM, Tobias Buschor
<tobias.buschor at gmail.com> wrote:
> I propose to change the arguments.
> The second argument "target" is optional and default to "Object.create(null)"
> The return-value is the modified target
> Advantages:
>  - Without the second argument, we can copy the source
>  - It is more natural to say "mixin the source into the target" => misin.(source, target)
> Disadvantages
>  - Its not suitable to extend the function to accept multiple sources in the future
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss

More information about the es-discuss mailing list