1
0
Fork 0
easejs/index.html

292 lines
9.2 KiB
HTML

<div class="headline">
<h3>Classical Object-Oriented<br />JavaScript Framework</h3>
<a href="download.html" class="download btn large go">
<div class="inner">
Download <!--%curver-->
<div class="note">Released: <!--%curver-date--></div>
</div>
</a>
<p>
<a href="https://www.gnu.org/" class="subtle" title="GNU's Not Unix!">GNU</a>
ease.js is a Classical Object-Oriented framework for JavaScript, intended to
eliminate boilerplate code and &ldquo;ease&rdquo; the transition into
JavaScript from other Object-Oriented languages. Features include:
</p>
<ul class="features">
<li><a href="#class-dfn">Simple and intuitive class definitions</a></li>
<li><a href="#inheritance">Classical inheritance</a></li>
<li><a href="#traits">Traits as mixins</a></li>
<li>
<a href="#access-modifiers">
Access modifiers (public, protected, and private)
</a>
</li>
<li><a href="#abstract">Abstract classes and methods</a></li>
<li><a href="#interfaces">Interfaces</a></li>
<li><a href="#static">Static and constant members</a></li>
<li><a href="#error">Transparent error subtyping</a></li>
<li>Support for ECMAScript 3+</li>
</ul>
</div>
<p class="info">
GNU ease.js is a framework, not a compiler. It may be used wherever JavaScript
may be used, and supports all major browsers; ease.js also provides support
for older, pre-ES5 environments by gracefully degrading features (such as
visibility support) while remaining functionally consistent.
</p>
<p class="info">
This project is part of the <a href="https://www.gnu.org/">GNU Project</a>.
</p>
<h3 id="class-dfn" class="bigemph">
Simple and Intuitive Class Definitions
<span class="anchor"><a href="#class-dfn">&para;</a></span>
</h3>
<p>
Class definitions closely resemble the familiar syntax of languages like Java
and PHP.
</p>
<pre class="js">
<!--%inc scripts/ex/class.js -->
</pre>
<p>
Classes can be
<a href="manual/Anonymous-vs_002e-Named-Classes.html" class="man">anonymous or
named</a>, the latter being more useful for debugging. Since classes may be
anonymous, constructors are <a
href="manual/Constructor-Implementation.html" class="man">styled after
PHP</a>.
</p>
<pre class="js">
<!--%inc scripts/ex/class-anon.js -->
</pre>
<p>
Classes can be instantiated with or without the <tt>new</tt> keyword. Omission
aids in concise method chaining and the use of
<a href="manual/Temporary-Instances.html" class="man">temporary instances</a>.
</p>
<pre class="js">
const inst_a = Foo( "John Doe" );
const inst_b = new Foo( "John Doe" );
// temporary instance
Foo( "John Doe" ).sayHello();
</pre>
<a href="manual/Defining-Classes.html">&rarr; Read more in manual</a>
<a href="#" class="top">&uarr;</a>
<h3 id="inheritance" class="bigemph">
Classical Inheritance
<span class="anchor"><a href="#inheritance">&para;</a></span>
</h3>
<p>
Classes can be extended to create subtypes. Like C++, methods are
<em>not</em> virtual by default. In Java terminology, all methods are
final by default. Multiple inheritance, like Java, is unsupported (see
<a href="#interfaces">Interfaces</a>).
</p>
<pre class="js">
<!--%inc scripts/ex/class-extend.js-->
</pre>
<p>
Alternatively, if creating an anonymous subtype, the supertype's
<tt>extend()</tt> method may be used.
</p>
<pre class="js">
const SturdyCow = Cow.extend( { /*...*/ } );
</pre>
<p>
Type checks for polymorphic methods may be performed with
<tt>Class.isA()</tt>, which is <a
href="manual/Type-Checks-and-Polymorphism.html" class="man">recommended in
place of <tt>instanceof</tt></a>.
</p>
<pre class="js">
<!--%inc scripts/ex/class-poly.js-->
</pre>
<p>
To prevent a class from being extended, <a href="manual/Final-Classes.html"
class="man"><tt>FinalClass</tt></a> may be used.
</p>
<pre class="js">
<!--%inc scripts/ex/class-final.js-->
</pre>
<a href="manual/Inheritance.html">&rarr; Read more in manual</a>
<a href="#" class="top">&uarr;</a>
<h3 id="traits" class="bigemph">
Traits As Mixins
<span class="anchor"><a href="#traits">&para;</a></span>
</h3>
<p>
Trait support was introduced in celebration of becoming a GNU
project. It is currently under development and has not yet been
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,
the <a href="http://git.savannah.gnu.org/cgit/easejs.git/tree/test/Trait">test
cases provide extensive examples and rationale</a>. The following posts
also summarize some of the features:
<ul>
<li><a href="news.html#ac1a036">Preliminary support for traits as mixins</a></li>
<li><a href="news.html#3005cda">Support for stacked mixins</a></li>
<li><a href="news.html#3fc0f90">Initial implementation of parameterized traits</a></li>
<li><a href="news.html#d9b86c1">Class supertype overrides</a></li>
</ul>
</p>
<a href="#" class="top">&uarr;</a>
<h3 id="access-modifiers" class="bigemph">
Access Modifiers
<span class="anchor"><a href="#access-modifiers">&para;</a></span>
</h3>
<p>
All three common access modifiers&mdash;public, protected and
private&mdash;are supported, but <a href="manual/Pre_002dES5-Fallback.html"
class="man">enforced only in ECMAScript 5 and later</a> environments.
</p>
<pre class="js">
<!--%inc scripts/ex/access-modifiers.js -->
</pre>
<p>
In the above example, the database connection remains encapsulated within
<tt>DatabaseRecord</tt>. Subtypes are able to query and escape strings and
external callers are able to retrieve a name for a given id. Attempting to
access a private or protected member externally will result in an error.
Attempting to access a private member from within a subtype will result in an
error.
</p>
<p>
Alternatively, a more concise style may be used, which is more natural to
users of JavaScript's native prototype model:
</p>
<pre class="js">
<!--%inc scripts/ex/access-modifiers-implicit.js -->
</pre>
<a href="manual/Access-Modifiers.html">&rarr; Read more in manual</a>
<a href="#" class="top">&uarr;</a>
<h3 id="abstract" class="bigemph">
Abstract Classes and Methods
<span class="anchor"><a href="#abstract">&para;</a></span>
</h3>
<p>
If a class contains abstract members, it must be declared as an
<tt>AbstractClass</tt>. Abstract methods must be overridden by subtypes and
are implicitly virtual.
</p>
<pre class="js">
<!--%inc scripts/ex/class-abstract.js-->
</pre>
<a href="manual/Abstract-Members.html">&rarr; Read more in manual</a>
<a href="#" class="top">&uarr;</a>
<h3 id="interfaces" class="bigemph">
Interfaces
<span class="anchor"><a href="#interfaces">&para;</a></span>
</h3>
<p>
ease.js supports the Java concept of Interfaces, which act much like abstract
classes with no implementation. Each method is implicitly abstract. Properties
cannot be defined on interfaces.
</p>
<pre class="js">
<!--%inc scripts/ex/interface.js -->
</pre>
<p>
Concrete classes may implement one or more interfaces. If a concrete class
does not provide a concrete implementation for every method defined on the
interface, it must be declared an <a
href="#abstract"><tt>AbstractClass</tt></a>.
</p>
<pre class="js">
<!--%inc scripts/ex/interface-2.js -->
</pre>
<p>
Polymorphic methods may check whether a given object implements a certain
interface.
</p>
<pre class="js">
<!--%inc scripts/ex/interface-3.js -->
</pre>
<a href="manual/Interfaces.html">&rarr; Read more in manual</a>
<a href="#" class="top">&uarr;</a>
<h3 id="static" class="bigemph">
Static and Constant Members
<span class="anchor"><a href="#static">&para;</a></span>
</h3>
<p>
Static members are bound to the class itself, rather than a particular
instance. Constants are immutable static members (unlike languages like PHP,
they may use any access modifier). In order to support both
<a href="manual/Static-Implementation.html" class="man">pre- and
post-ECMAScript 5 environments</a>, the syntax requires use of a static
accessor method&mdash;<tt>$()</tt>.
</p>
<pre class="js">
<!--%inc scripts/ex/static-members.js -->
</pre>
<a href="manual/Static-Members.html">&rarr; Read more in manual</a>
<a href="#" class="top">&uarr;</a>
<h3 id="error" class="bigemph">
Transparent Error Subtyping
<span class="anchor"><a href="#error">&para;</a></span>
</h3>
<p>
Error subtyping (creating your own error types) in ECMAScript is
notoriously crude, and getting it to work intuitively is even
harder. ease.js transparently handles all necessarily boilerplate when
extending <tt>Error</tt> or its subtypes.
</p>
<pre class="js">
<!--%inc scripts/ex/error-subtype.js -->
</pre>
<a href="manual/Error-Subtypes.html">&rarr; Read more in manual</a>
<a href="#" class="top">&uarr;</a>
<script type="text/javascript" src="scripts/highlight.pack.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3-or-Later */
try
{
// JS syntax highlighting, ES5
Array.prototype.slice.call( document.querySelectorAll( '.js' ) )
.forEach( function( element )
{
hljs.highlightBlock( element, ' ' );
} );
}
catch ( e ) {};
/* @license-end */
</script>