Object.define ==> Object.mixin??

Allen Wirfs-Brock allen at wirfs-brock.com
Tue Dec 11 10:12:04 PST 2012


On Dec 11, 2012, at 10:00 AM, Rick Waldron wrote:

> 
> 
> 
> On Tue, Dec 11, 2012 at 12:28 PM, Allen Wirfs-Brock <allen at wirfs-brock.com> wrote:
> I'm the past we discussed issues surrounding the semantic differences between "put" and "define" and we've agreed to include Object.assign in ES6.  We have also discussed Object.define but have not yet made a decision to include it.
> 
> Nicholas Zaka recently posted a short article that addresses issues relating to the assign/define distinction http://www.nczonline.net/blog/2012/12/11/are-your-mixins-ecmascript-5-compatible/  as they already surface in ES5.
> 
> For me, this article reenforces that we really need to have something like Object.define in ES6.
> 
> 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:

Except,
1) It needs to iterate own keys, not just string valued property names so it will pick up properties whose keys are symbols
2) It needs to rebind super references
3) I don't see any reason that it should be restricted to enumerable properties. If the intend is to deprecate enumerable along with for-in then we should be adding new functionality that is sensitive to the state of the enumerable attribute.

Allen




> 
> Object.mixin = function(receiver, supplier) {
>   return Object.keys(supplier).reduce(function(receiver, property) {
>     return Object.defineProperty(
>       receiver, property, Object.getOwnPropertyDescriptor(supplier, property)
>     );
>   }, receiver);
> };
> 
> 
> var a = {}, name = "Rick";
> 
> var b = Object.mixin(a, {
>   get name() {
>     return name;
>   }
> });
> 
> console.log( a.name ); // "Rick"
> console.log( b.name ); // "Rick"
> console.log( a === b ); // true
> 
> 
> Rick
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20121211/f92058a1/attachment.html>


More information about the es-discuss mailing list