ES6-style constructor documentation
Shame on me for not including this in the previous merge!master
commit
50500ed290
|
@ -323,17 +323,22 @@ class is causing the issue.
|
||||||
|
|
||||||
@node Constructors
|
@node Constructors
|
||||||
@subsection Constructors
|
@subsection Constructors
|
||||||
In JavaScript, we are used to functions themselves being a constructor
|
A ``constructor'' in JavaScript is simply a function---whether or not
|
||||||
because only constructors can be instantiated. With ease.js, constructors
|
it actually constructs a new object depends on whether the @code{tt}
|
||||||
are handled in a manner similar to most other languages, by providing a
|
keyword is used. With ease.js, constructors are handled in a manner
|
||||||
separate method. The implementation ease.js chose is very similar to that of
|
similar to most other languages: by providing a separate method.
|
||||||
PHP's (@pxref{Constructor Implementation}).
|
|
||||||
|
Until the release of ECMAScript@tie{}6, which introduced the @code{class}
|
||||||
|
keyword, there was no convention for constructors defined in this
|
||||||
|
manner. The implementation ease.js chose is very similar to that of
|
||||||
|
PHP's (@pxref{Constructor Implementation}):
|
||||||
|
|
||||||
@float Figure, f:constructor
|
@float Figure, f:constructor
|
||||||
@verbatim
|
@verbatim
|
||||||
var Foo = Class( 'Foo',
|
var Foo = Class( 'Foo',
|
||||||
{
|
{
|
||||||
'public __construct': function( name )
|
// may also use `construct`; see below
|
||||||
|
__construct: function( name )
|
||||||
{
|
{
|
||||||
console.log( 'Hello, ' + name + '!' );
|
console.log( 'Hello, ' + name + '!' );
|
||||||
}
|
}
|
||||||
|
@ -348,6 +353,31 @@ PHP's (@pxref{Constructor Implementation}).
|
||||||
@caption{Declaring constructors using ease.js}
|
@caption{Declaring constructors using ease.js}
|
||||||
@end float
|
@end float
|
||||||
|
|
||||||
|
ease.js introduced the @code{constructor} method in version@tie{}0.2.7
|
||||||
|
to match the ES6 ``class'' implementation; it is an alias for
|
||||||
|
@code{__construct}. This method name may be used prior to ES6.
|
||||||
|
|
||||||
|
@float Figure, f:constructor-es6
|
||||||
|
@verbatim
|
||||||
|
// ECMAScript 6 syntax
|
||||||
|
let Foo = Class( 'Foo',
|
||||||
|
{
|
||||||
|
// you may still use __construct
|
||||||
|
constructor( name )
|
||||||
|
{
|
||||||
|
console.log( 'Hello, ' + name + '!' );
|
||||||
|
}
|
||||||
|
} );
|
||||||
|
|
||||||
|
// instantiate the class, invoking the constructor
|
||||||
|
Foo( 'World' );
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// Hello, World!
|
||||||
|
@end verbatim
|
||||||
|
@caption{Declaring constructors in an ECMAScript 6 style}
|
||||||
|
@end float
|
||||||
|
|
||||||
When the class is instantiated, the constructor is invoked, permitting you
|
When the class is instantiated, the constructor is invoked, permitting you
|
||||||
do to any necessary initialization tasks before the class can be used. The
|
do to any necessary initialization tasks before the class can be used. The
|
||||||
constructor operates exactly how you would expect a constructor to in
|
constructor operates exactly how you would expect a constructor to in
|
||||||
|
@ -1148,7 +1178,7 @@ Therefore, we will use a static property of class @var{BigBang}.
|
||||||
*
|
*
|
||||||
* @return {undefined}
|
* @return {undefined}
|
||||||
*/
|
*/
|
||||||
'public __construct': function( type, data )
|
__construct: function( type, data )
|
||||||
{
|
{
|
||||||
this._type = type;
|
this._type = type;
|
||||||
|
|
||||||
|
@ -1313,7 +1343,7 @@ mounts from @file{/etc/fstab}:
|
||||||
'private _mountPoints': [],
|
'private _mountPoints': [],
|
||||||
|
|
||||||
|
|
||||||
'public __construct': function()
|
__construct: function()
|
||||||
{
|
{
|
||||||
var data = fs.readFileSync( this.$('_PATH') );
|
var data = fs.readFileSync( this.$('_PATH') );
|
||||||
this._parseMountPoints( data );
|
this._parseMountPoints( data );
|
||||||
|
|
Loading…
Reference in New Issue