1
0
Fork 0

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
Mike Gerwitz 2017-11-07 00:14:56 -05:00
parent 23ceff79ca
commit 0b50ee295f
Signed by: mikegerwitz
GPG Key ID: 8C917B7F5DC51BA2
2 changed files with 60 additions and 0 deletions

View File

@ -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,

View File

@ -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