diff --git a/index.html b/index.html index 1ac4eb9..a6bbf50 100644 --- a/index.html +++ b/index.html @@ -137,6 +137,9 @@ finalized, but has been included in each GNU ease.js release since v0.2.0, and is stable.

+
+  
+

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