From 90aa74d0cb3bb0bd344a0de25699052e9c2e70fa Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Wed, 16 Sep 2015 00:37:24 -0400 Subject: [PATCH 1/2] ES6-style constructor added to documentation --- doc/classes.texi | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/doc/classes.texi b/doc/classes.texi index 7f5c28b..748b3bb 100644 --- a/doc/classes.texi +++ b/doc/classes.texi @@ -323,17 +323,22 @@ class is causing the issue. @node Constructors @subsection Constructors -In JavaScript, we are used to functions themselves being a constructor -because only constructors can be instantiated. With ease.js, constructors -are handled in a manner similar to most other languages, by providing a -separate method. The implementation ease.js chose is very similar to that of -PHP's (@pxref{Constructor Implementation}). +A ``constructor'' in JavaScript is simply a function---whether or not +it actually constructs a new object depends on whether the @code{tt} +keyword is used. With ease.js, constructors are handled in a manner +similar to most other languages: by providing a separate method. + +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 @verbatim var Foo = Class( 'Foo', { - 'public __construct': function( name ) + // may also use `construct`; see below + __construct: function( name ) { console.log( 'Hello, ' + name + '!' ); } @@ -348,6 +353,31 @@ PHP's (@pxref{Constructor Implementation}). @caption{Declaring constructors using ease.js} @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 do to any necessary initialization tasks before the class can be used. The constructor operates exactly how you would expect a constructor to in From 0e26f9fd0f5c98748d54f5237eedac343df14f1b Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Wed, 16 Sep 2015 00:38:04 -0400 Subject: [PATCH 2/2] Strip `public` keyword from __construct in docs It must be public, so by convention, I don't include it anymore. But this is personal preference. --- doc/classes.texi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/classes.texi b/doc/classes.texi index 748b3bb..d35fc04 100644 --- a/doc/classes.texi +++ b/doc/classes.texi @@ -1178,7 +1178,7 @@ Therefore, we will use a static property of class @var{BigBang}. * * @return {undefined} */ - 'public __construct': function( type, data ) + __construct: function( type, data ) { this._type = type; @@ -1343,7 +1343,7 @@ mounts from @file{/etc/fstab}: 'private _mountPoints': [], - 'public __construct': function() + __construct: function() { var data = fs.readFileSync( this.$('_PATH') ); this._parseMountPoints( data );