time to trim mustache

Brandon Benvie brandon at brandonbenvie.com
Wed Jun 6 04:33:26 PDT 2012


Object.decorate seems to fit the bill I think.

On another note, using an array and the as of yet non-standardized but
still useful function name property is a lot more palatable and succinct
and is much closer to the desired improved object literal syntax. I've been
partial to the following reimagining of Object.extend as Object.decorate
which supports arrays of named functions as well as objects and also
setters/getters which existing extend functions don't support usually.

  Object.defineProperty(Object, 'decorate', {
    configurable: true,
    writable: true,
    value: function decorate(o){
      var a, b, c, d;
      for (a in arguments) {
        if (a) {
          if (Array.isArray(b = arguments[a])) {
            // array of named functions
            for (c = 0; c < b.length; c++) {
              if (typeof b[c] === 'function' && b[c].name) {
                Object.defineProperty(o, b[c].name, { value: b[c],
configurable: true, writable: true });
              }
            }
          } else {
            // object
            for (c in b) {
              // use getDesc instead of hasOwn to support get/set
              if (d = Object.getOwnPropertyDescriptor(b, c)) {
                if (d.get || d.set) {
                  Object.defineProperty(o, c, d);
                } else {
                  // purposefully trigger accessors if they exist
                  o[c] = d.value;
                }
              }
            }
          }
        }
      }
      return o;
    }
  });
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120606/62a73fd9/attachment.html>


More information about the es-discuss mailing list