- A multi-paradigm, interpreted language that was
originally intended for browser-side web programming
- Based on the ECMAScript specification that
intends to standardize client-side scripting
- Starting to be used for more non-client applications like web
servers and databases
- Even being used for non-web applications like word
processors and PDF support
JavaScript supports object-orientation but differs
from
Java in significant ways:
Java | JavaScript |
Consists of classes and their instances (objects) |
Consists of objects only, with classes simulated
(ECMAScript 2015 introduced class notation) |
Statically typed: Variables must have data types |
Dynamically typed: Variables do not have data types |
Strongly typed: Most type rule violations detected by compiler |
Loosely (or weakly) typed: Errors detected at run-time |
Byte-compiled language executed by a virtual machine |
Human-readable language executed by an interpreter (or
JavaScript engine) |
Large language designed for fast execution and type safety |
Lightweight language designed for embedding in other
applications, such as web browsers |
This section shows more JavaScript code examples from the
farmer-wolf-goat-cabbage (FWGC) problem.
JavaScript objects for the FWGC problem are similar to those in our
Java implementation. There are three files:
- FarmerState.js
- FarmerMover.js
- FarmerProblem.js
whose objectives correspond to their Java counterparts.
However, there are no class or interface definitions and, therefore,
there is no class or interface inheritance as in our Java framework.
State objects are represented using
FarmerState.js, shown in the menu.
Note the inclusion of
equals and
safe methods along
with
toString.
Shown below is
StateTest.html, which creates and manipulates
some
FarmerState objects.
Here is how the browser renders
StateTest.html.
Move objects are represented using
FarmerMover.js, shown in the menu.
Note the
doMove method
for objects created with the
FarmerMover function, as well as
non-method functions used to support
doMove.
Shown below is
MoverTest.html, which creates and manipulates
a
FarmerMover object:
Here is how the browser renders
MoverTest.html.
Problem objects are represented using
FarmerProblem.js, shown in
the menu.
Note the inclusion of familiar properties and methods for problem objects.
Shown below is
ProblemTest.html, which creates and manipulates
a
FarmerProblem object.
It shows a use of the
map method available from JavaScript's
predefined core
Array object.
map takes a function as an argument and applies it to each
element of the implicit array (in this case,
problem.moveNames),
producing a new array containing the results. (Compare the
map
method for Java streams.)
Here is how the browser renders
ProblemTest.html.
The water jug JavaScript files have the same organization as the FWGC
files.
Although the corresponding files do not share formal interface types or
extend the same abstract classes, there is commonality:
- The WaterJugState.js and FarmerState.js files both
provide the equals and toString methods
- The WaterJugMove.js and FarmerMove.js files both
provide the doMove method
- The WaterJugProblem.js and FarmerProblem.js files
both provide the success method and
the name, introduction, initialState, currentState,
and moves properties
This commonality will be exploited by JavaScript code that
implements an interactive problem solver similar to our Java problem solving
framework's
GUI class.
Understanding that code requires more understanding of HTML and the DOM.