Javascript already support extension so what real supplement advantages would jstraits http://code.google.com/p/jstraits/ would bring to it ?
Javascript already support extension so what real supplement advantages would jstraits http://code.google.com/p/jstraits/ would bring
Share
Basically, Traits provides a way for unrelated classes to share code.
here’s an (admittedly contrived) example: A class that draws a bouncing ball and a class that draws a happy face emoticon both need to draw a circle. They probably don’t share a common super class. So, they can’t inherit the circle drawing code. Traits lets you put the circle drawing code in
TShapeand say that each usesdrawCirclefromTShape. This leaves you with all the circle drawing code inTShaperather than each class having it’s own implementation.(Like I said contrived. For drawing you’d call into a common library. This is just for illustration purposes.)
Now, you can certainly do this yourself by creating a function and assigning it into each prototype. However that will become unwieldy as your codebase grows.
My rule of thumb to keep things manageable is traits can use each other however makes sense, but otherwise I try to only use traits in directly instantiable classes. Having class
Ausing traitXand classBinheriting fromAand using traitYcan get hairy to reason about. Especially if any renaming takes place.