ES6-style constructor added to documentation
parent
cbf98cccf1
commit
90aa74d0cb
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue