Traits as prototypes. Valid approach?

14 views
Skip to first unread message

Thiago F

unread,
Jan 25, 2016, 11:40:28 PM1/25/16
to traits.js
I have been trying traits.js and looking for how they would best work for me.

Right now frozen objects does not seem a good fit (though they will be great for other stuff).

Other than that traits.js seem to be a way to create property descriptors to use with Object.create (which is great by itself). And then said objects will have all properties and methods described.

How about using traits.js to create the prototypes? Perhaps defining only the methods. And then use Object.create again with the created prototype object. This way we will not recreate the methods for each instance.

Does it make sense?

I am still trying to wrap my head around traits.js (Traits, I mean). I think they offer me great possibilities but I am still not sure how to use them for real... I think I have to study them more. =)

The first file on this gist shows what I tried: https://gist.github.com/slacktracer/63fff6e69c4e263ada3b

It kinda works. It pretty much works I mean. Perhaps I am missing something though.


Thank you.


Cheers,
Thiago F

Tom Van Cutsem

unread,
Jan 31, 2016, 1:43:44 PM1/31/16
to trai...@googlegroups.com
Hi,

Yes, traits can serve as prototypes for objects, and it is intentional that Object.create works fine when using a trait as a prototype. In fact, if you don't want frozen objects, then that's exactly what Object.create is for (Trait.create creates "high-integrity" frozen objects, while Object.create creates "normal" JS objects).

However, what you *cannot* do is to create a trait *instance*, and then use the trait instance as the prototype of yet other objects. While it's true that this would avoid instantiating the methods per instance, it won't work because Trait.create automatically binds |this| to the trait instance, making it impossible for sub-objects to reliably override the methods (the |this| in the trait instance will not point to the child object).


Use Trait.create to instantiate objects that should be considered "final" and complete. Use Object.create to instantiate objects that can remain "abstract" or otherwise extensible. For "final" objects, as generated by Trait.create, keep in mind that the this pseudovariable within their methods is bound at instantiation time to the instantiated object. This implies that final objects don't play nice with objects that delegate to them (this is no longer late bound for such objects). That's why we call them final: such objects should not serve as the prototype of other objects.

Hope this helps.

Cheers,
Tom

--
You received this message because you are subscribed to the Google Groups "traits.js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to traits-js+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Thiago F

unread,
Feb 2, 2016, 10:38:39 AM2/2/16
to traits.js
It did help. A lot. Thank you very much! =)
Reply all
Reply to author
Forward
0 new messages