Object.mixin rather than Object.getOwnPropertyDescriptors

Marcus Stade marcus at stade.se
Wed Apr 30 14:52:19 PDT 2014


I have a use case for `Object.getOwnPropertyDescriptors` – it may or may
not be to your liking, but I have one. I'm currently implementing a library
to help with employing more pure functional programming techniques in
JavaScript environments. This includes such things as (mostly) forcing
immutability, devising copy-on-write schemes etc. One thing in particular
that I'm devising is a kind of Type object, which is little more than a set
of fields to provide structural guarantees and some rudimentary semantic
abilities (is-a type stuff.) In constructing these, I'm using an API that
looks something like this:

```
const MyType = deftype("MyType", BaseType,
  { field: 1
  , get something() { return this.x }
  , set something(val) { this.x = val }
  }
)

const myInstance = MyType({ field: 5 })
```

The third argument to the `deftype` function is an object – any object –
where the fields are inspected and stored as a sort of template for
subsequent instance creation. The field descriptors are modified to always
be non-configurable, and for fields that aren't a get/set combo they're
also always set to be immutable. Additionally, I'm doing some rebinding of
functions such that they have an implicit internal scope (hence the `this`
in the accessors above) which makes it a lot easier to reason about
internal state. Currently the way this is done is through a combination of
`getOwnPropertyNames` and `getOwnPropertyDescriptor`.

I'd have to agree with Andrea and say – somewhat rhetorically – why not? It
seems like a harmless addition. That said, that it doesn't exist is mostly
just an annoyance.

-- Marcus


On Wed, Apr 23, 2014 at 12:20 AM, Rick Waldron <waldron.rick at gmail.com>wrote:

>
>
>
> On Tue, Apr 22, 2014 at 1:27 PM, Claude Pache <claude.pache at gmail.com>wrote:
>
>> Hi,
>>
>> There has been request to add `Object.getOwnPropertyDescriptors` (plural)
>> to the standard. Reviewing use cases presented in thread [1] or in older
>> thread [2], it seems to me that all of them boil down to copy all own
>> properties of one object to another, e.g.,
>>
>>     Object.defineProperties(target,
>> Object.getOwnPropertyDescriptors(source))
>>     Object.create(proto, Object.getOwnPropertyDescriptors(source))
>>     // etc.
>>
>> However, this is exactly what `Object.mixin` (deferred from ES6) was
>> designed for:
>>
>>     Object.mixin(target, source)
>>     Object.mixin(Object.create(proto), source)
>>     // etc.
>>
>> Besides being shorter to write, `Object.mixin` has the advantages of (1)
>> not creating an intermediate object; (2) taking care of some subtleties,
>> like rebinding `super` for methods, getters and setters if needed.
>>
>> Therefore, I think that `Object.mixin` is a better function to have than
>> `Object.getOwnPropertyDescriptors`.
>>
>
> The latter is not replacing the former; they are different APIs for
> different use cases. For example, Object.mixin cannot be used to derive
> descriptors from an object and filter then properties by descriptor
> attribute value:
>
>   // module.js
>   function descriptorsByAttributeFilter(o, filter = {}) {
>     var descriptors = Object.getOwnPropertyDescriptors(o);
>     var filters = Object.keys(filter);
>
>     return Object.keys(descriptors).reduce((accum, key) => {
>       var descriptor = descriptors[key];
>
>       if (filters.every(field => descriptor[field] === filter[field])) {
>         accum[key] = descriptor;
>       }
>       return accum;
>     }, {});
>   }
>
>   export function writables(o) {
>     return descriptorsByAttributeFilter(o, { writable: true });
>   }
>
>   export function nonwritables(o) {
>     return descriptorsByAttributeFilter(o, { writable: false });
>   }
>
>
>   // script.js
>
>   import {writables, nonwritables} from "module";
>
>   var o = Object.defineProperties({}, {
>     thisisnonwritable: {
>       value: 1, writable: false
>     },
>     thisiswritable: {
>       value: 2, writable: true
>     }
>   });
>
>   console.log( writables(o) );
>   console.log( nonwritables(o) );
>
>
> Rick
>
>
> _______________________________________________
> 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/20140430/9b585400/attachment.html>


More information about the es-discuss mailing list