Engineering Full Stack Apps with Java and JavaScript
JavaScript (JS) is a dynamic computer programming language which is most commonly used as part of web browsers to allow client-side scripts to interact with the user, control the browser, communicate asynchronously, and alter the document content that is displayed. JavaScript is however not limited to web browsers and applies to other areas as well like PDF documents, VXML documents and desktop widgets.
JavaScript was formalized in the ECMAScript language standard. ECMAScript is the scripting language standardized by Ecma International in the ECMA-262 specification and JavaScript, JScript and ActionScript can be considered as the implementations for ECMAScript. Some browsers and applications provide additional functionalities than specified in the ECMAScript, but it is always better to comply with ECMAScript.
JavaScript has no relation to Java programming language except for the similarity in name and some similarity in syntax. JavaScript is dynamically typed compared to Java which is strongly typed. JavaScript was originally developed by Brendan Eich of Netscape under the name Mocha, later LiveScript, and finally renamed to JavaScript due to the popularity of Java programming language.
A dynamic web page is created basically using three building blocks and they are HTML, CSS and JavaScript. HTML provides the markup and is static. CSS provides the look and feel independent of the markup; for instance, you can have different look and feel to the same HTML page by changing the CSS associated with it. And finally JavaScript give dynamic features to the page such as allow client-side scripts to interact with the user, control the browser, communicate asynchronously, and alter the HTML content that is displayed.
Enough of theory and explanations; we will quickly go technical and see some useful stuff for working with JavaScript. This article assumes that you are familiar with Java or any programming language and common constructs like variables, arrays etc. Else, please go through ‘Beginning Java Book’.
First we will see a simple “Hello World” program.
Create an HTML file and place the below script tag and contents in its head:
<script type="text/javascript">
alert("Hello World!")
</script>
You can either type all html tags or use an IDE like eclipse which will create an html page with basic tags. I will add this to an eclipse generated HTML file as:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Hello World</title>
<script type="text/javascript">
alert("Hello World!")
</script>
</head>
<body>
</body>
</html>
When I open this is a browser, the script within the head is executed and I will get a popup with the test “Hello World!”
Now let us see some important rules and semantics of JavaScript:
JavaScript is a case sensitive.
JavaScript statements have to be included inside the <script> tags. The script tags can either be added to the head tag or to the body tag.
Browsers will need to have a built-in JavaScript engine that interprets and executes the JavaScript on the webpage. Examples of such engines are SpiderMonkey and Rhino.
A JavaScript will block the page until they are loaded.
The keyword var is used to declare a variable in JavaScript, followed by the name of that variable:
var val=5;
Until a value is assigned to a variable, the value of the variable will be implicitly set to undefined. Also note that there is no type assigned to the variable and it takes the type of the value assigned to it. JavaScript is dynamically typed compared to Java which is strongly typed.
A JavaScript array is similar to a C or java array and contains ordered elements with an index for every element. We can declare, provide values and later retrieve values as:
var myArray = [];
myArray[0] = "zero";
myArray[1] = "one";
alert(myArray[1]);
Here the index doesn’t have to be integers and can be even strings:
var myArray = [];
myArray["zero"] = "first";
myArray["one"] = "second";
alert(myArray["one"]);
We can also mix the index and value types:
var myArray = [];
myArray[0] = "first";
myArray["one"] = 1;
alert(myArray[0]);
alert(myArray["one"]);
JavaScript also supports multidimensional arrays like Java. Multidimensional arrays are arrays that contain arrays:
var myArray1 = [];
myArray1[0] = "first";
myArray1[1] = "second";
var myArray2 = [];
myArray2[0] = "secondfirst";
myArray2[1] = "secondsecond";
var multiArray = [myArray1,myArray2];
alert(multiArray[1][1]);
This will alert with value “secondsecond”.
JavaScript objects are derived from the object-oriented programming concept. But unlike Java, you don’t have to define a class first with properties and methods. Properties can be created as and when required.
var emp = new Object();
emp.name="Heartin";
emp.age=29;
emp.showDetails = function (){
alert ("Name="+ emp.name+". Age="+emp.age);
}
emp.showDetails();
We also saw how to define a function within an object and how to invoke them.
We can also define objects by using the curly braces:
var emp ={
"name" : "Heartin",
"age" : 29,
"showDetails" : function (){
alert ("Name="+ this.name+". Age="+this.age);
},
};
emp.showDetails();
From my experience, this syntax is more prone to errors than the previous one. Also, note that unlike Java, JavaScript won’t give you a stack trace of the error, but will fail silently. So checkout the minute details like missing elements such as comma, semicolon, equalto etc.
Until Java SE 7, JDKs shipped with a JavaScript scripting engine based on Mozilla Rhino. Java SE 8 will instead ship with a new engine called Oracle Nashorn, which is based on JSR 292 and invokedynamic. It provides better compliance with the ECMA normalized JavaScript specification and better runtime performance.
http://en.wikipedia.org/wiki/JavaScript
http://www.w3schools.com/js/DEFAULT.asp
http://www.oracle.com/technetwork/articles/java/jf14-nashorn-2126515.html