Object.define ==> Object.mixin??

Brandon Benvie brandon at brandonbenvie.com
Wed Dec 12 09:42:04 PST 2012


Ah yes errors. It should probably have similar semantics to how
defineProperties works now...attempting each property and holding errors
until the end, no? Same for assign.

function mixin(target, source){
  if (Type(target) !== 'Object') throw new TypeError ("target must be an
object");
  if (Type(source) !== 'Object') throw new TypeError("source must be an
object");

  var error;
  Object.keys(source).forEach(function(key){ // or getOwnPropertyNames?
    try {
      Object.defineProperty(target, key,
Object.getOwnPropertyDescriptor(source, key));
    } catch (e) {
      error || (error = e);
    }
  });

  if (error) throw error;
  return target;
}

On Wednesday, December 12, 2012, Andrea Giammarchi wrote:
>
>
> 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 ?
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20121212/090b3300/attachment.html>


More information about the es-discuss mailing list