Add trait example to index page
Long overdue...considering this is ease.js' most attractive and unmatched feature. * scripts/ex/trait.js: New example. * index.html: Use it.website
parent
23ceff79ca
commit
0b50ee295f
|
@ -137,6 +137,9 @@
|
|||
finalized, but has been included in each GNU ease.js release since v0.2.0,
|
||||
and is stable.
|
||||
</p>
|
||||
<pre class="js">
|
||||
<!--%inc scripts/ex/trait.js-->
|
||||
</pre>
|
||||
<p>
|
||||
Documentation will be available once some final details are
|
||||
finalized. Until that time,
|
||||
|
|
|
@ -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
|
Loading…
Reference in New Issue