From 0b50ee295ffcba32e4c5ee937b60739758345831 Mon Sep 17 00:00:00 2001
From: Mike Gerwitz
+ +
Documentation will be available once some final details are finalized. Until that time, diff --git a/scripts/ex/trait.js b/scripts/ex/trait.js new file mode 100644 index 0000000..169338f --- /dev/null +++ b/scripts/ex/trait.js @@ -0,0 +1,57 @@ +const Echo = Class( 'Echo', +{ + 'virtual public echo'( str ) + { + return str; + }, +} ); + +const Prefix = Trait( 'Prefix' ) + .extend( Echo, +{ + 'private _prefix': '', + + __mixin( prefix ) + { + this._prefix = ''+prefix; + }, + + 'public abstract override echo'( str ) + { + return this._prefix + this.__super( str ); + }, +} ); + +const Suffix = Trait( 'Suffix' ) + .extend( Echo, +{ + 'private _suffix': '', + + __mixin( suffix ) + { + this._suffix = ''+suffix; + }, + + 'public abstract override echo'( str ) + { + return this.__super( str ) + this._suffix; + }, +} ); + +const UpperCase = Trait( 'UpperCase' ) + .extend( Echo, +{ + 'public abstract override echo'( str ) + { + return this.__super( str ).toUpperCase(); + } +} ); + +// stackable, parameterized traits +Echo.use( Prefix( "Bar" ) ) + .use( Suffix( "Baz" ) ) + .use( UpperCase ) + .use( Prefix( "Foo" ) ) + .use( Suffix( "Quux" ) )().echo( "Inner" ); + +// result: FooBARINNERBAZQuux