238 lines
7.3 KiB
HTML
238 lines
7.3 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 “ease” 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="#abstract">Abstract classes and methods</a></li>
|
|
<li><a href="#interfaces">Interfaces</a></li>
|
|
<li>
|
|
<a href="#access-modifiers">
|
|
Access modifiers (public, protected and private)
|
|
</a>
|
|
</li>
|
|
<li><a href="#static">Static and constant members</a></li>
|
|
<li>
|
|
Traits as mixins
|
|
<a href="http://git.savannah.gnu.org/cgit/easejs.git/?h=traits"
|
|
class="subtle">(under development)</a>
|
|
</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">¶</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">
|
|
var inst = Foo( "John Doe" );
|
|
var inst = new Foo( "John Doe" );
|
|
|
|
// temporary instance
|
|
Foo( "John Doe" ).sayHello();
|
|
</pre>
|
|
<a href="manual/Defining-Classes.html">→ Read more in manual</a>
|
|
<a href="#" class="top">↑</a>
|
|
|
|
|
|
<h3 id="inheritance" class="bigemph">
|
|
Classical Inheritance
|
|
<span class="anchor"><a href="#inheritance">¶</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">
|
|
var 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">→ Read more in manual</a>
|
|
<a href="#" class="top">↑</a>
|
|
|
|
|
|
<h3 id="abstract" class="bigemph">
|
|
Abstract Classes and Methods
|
|
<span class="anchor"><a href="#abstract">¶</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">→ Read more in manual</a>
|
|
<a href="#" class="top">↑</a>
|
|
|
|
|
|
<h3 id="interfaces" class="bigemph">
|
|
Interfaces
|
|
<span class="anchor"><a href="#interfaces">¶</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">→ Read more in manual</a>
|
|
<a href="#" class="top">↑</a>
|
|
|
|
|
|
<h3 id="access-modifiers" class="bigemph">
|
|
Access Modifiers
|
|
<span class="anchor"><a href="#access-modifiers">¶</a></span>
|
|
</h3>
|
|
<p>
|
|
All three common access modifiers—public, protected and
|
|
private—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>
|
|
<a href="manual/Access-Modifiers.html">→ Read more in manual</a>
|
|
<a href="#" class="top">↑</a>
|
|
|
|
|
|
<h3 id="static" class="bigemph">
|
|
Static and Constant Members
|
|
<span class="anchor"><a href="#static">¶</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—<tt>$()</tt>.
|
|
</p>
|
|
<pre class="js">
|
|
<!--%inc scripts/ex/static-members.js -->
|
|
</pre>
|
|
<a href="manual/Static-Members.html">→ Read more in manual</a>
|
|
<a href="#" class="top">↑</a>
|
|
|
|
|
|
<script type="text/javascript" src="scripts/highlight.pack.js"></script>
|
|
<script type="text/javascript">
|
|
try
|
|
{
|
|
// JS syntax highlighting, ES5
|
|
Array.prototype.slice.call( document.querySelectorAll( '.js' ) )
|
|
.forEach( function( element )
|
|
{
|
|
hljs.highlightBlock( element, ' ' );
|
|
} );
|
|
}
|
|
catch ( e ) {};
|
|
</script>
|