Java and Java Scripts are not the same. Java Script shares most of Java's expression syntax and basic control flow constructs. JavaScript is an interpreted scripting language, while Java compiles applications in a system of classes. The following table compares and contrasts JavaScript and Java.
JavaScript | Java |
---|---|
Interpreted (not compiled) by client. | Compiled on server before execution on client. |
Object-based. Code uses built-in, extensible objects, but no classes or inheritance. | Object-oriented. Applets consist of object classes with inheritance. |
Code integrated with, and embedded in, HTML. | Applets distinct from HTML (accessed from HTML pages). |
Variable data types not declared (loose typing). | Variable data types must be declared (strong typing). |
Dynamic binding. Object references checked at run-time. | Static binding. Object references must exist at compile-time. |
Secure. Cannot write to hard disk. | Secure. Cannot write to hard disk. |
1. Using Java Script in HTML (how to include your scripts in a page).
2. Creating simple JavaScripts (the fun stuff)
3. Objects, properties and methods (the hard stuff).
An object in Java Script is an associative array (see Essential Perl). It can also be thought as a function that defines an object type. In order to make this clear, I will use an unortodox approach and define functions before I even introduce variables.
function obj (v1, v2,..., vn) {
this.v1=key1;
this.v2=key2;
....................
this.vn=keyn;
}
As I said before, objects are associative arrays. The object definition creates not only the object type but also the variables, and assigns the key (in the key=value pair) to the variables. These keys/variables become the properties of the object. Lets see an example of creating a student object.
Function student (ss-no,name,gpa) {
this.ss-no=ss-no;
this.name=name;
this.gpa=gpa;
}
But how do we assign values to the properties? This happens when you instantiate (create an instance) of the object type. In the above example:
marty = new student("123-45-6789", "Marty Hayes", "3.95") . Marty.gpa is the value 3.95.
Example: let as consider the object home_computer. Its definition could be like:
function home_computer(brand,memory,year) {
this.brand=brand;
this.memory=memory;
this.year=year;
}
and for a student Marty it could be instantiated as:
marty_computer = new home_computer ("Mac","8Meg","1994");
The value of marty_computer.brand would be Mac.
In our example we would add the new method computer to the object student using:
student.computer = home_computer;
And to add to marty (of the student object type) the value of the marty_computer (of the home_computer object type) we woul use:
marty.computer = marty_computer;
Finally, you use the pattern object.methodname.property; to obtain the value of the new method for the object. In our example it would be: marty.computer.brand and the value would be Mac.
4. A little bit of syntax . Read later, at your leisure, all the details, as you need them.
5. Learning more JavaScript (the more involved stuff)
This page is maintained by Al Bento who can be reached at abento@ubmail.edu. This page was last updated on May 17, 2007. Although we will attempt to keep this information accurate, we can not guarantee the accuracy of the information provided.