Wednesday, February 15, 2006

Javascript program structure

Javascript programs can be structure in various different ways because it is one of the few languages that is both structured and object oriented. Some differences between structured and object oriented are the use of different terminology to describe the parts of our code. For example, in structured we have variables and in object oriented we have objects. In structured there are functions and in object oriented there are methods. However, in object oriented programming there are classes that contain the definitions for properties that objects belonging to this class will have and they are declared private. This means that the only way to change the value of these properties is through the public member functions declared in the class definition. By protecting the private variables, a programmer can avoid complications that arise in long programs like accidentally modifying the value of a variable. Object oriented programming still uses if statements and loops like structured programming but it contains them in methods belonging to one of the objects. This style of programming is much easier, clearer, and more powerful when dealing with a lot of code.

Thursday, February 09, 2006

JavaScript Statements & Expressions

Statements-

A small collection of commands including if, while, and for which are combined with objects, properties, methods, and events to round out the JavaScript language. These statements work independently from any object so you can use them whenever you want to. JavaScript provides the bare essential statements to produce a fully functional application. The eleven statements that JavaScript provides are as follows: // comment, break, continue, for, for…in, function, if…else, new, return, var, while, with. It does not implement the switch statement like Java does but it may in the future.

http://www.javaworld.com/jw-12-1996/jw-12-javascript.html

Expressions-

An expression is any valid set of literals, variables, operators, and expressions that evaluates to a single value. The value may be a number, a string, or a logical value. There are two types of expressions: assignment operators (assign a value to a variable) and plain operators (simply have a value). JavaScript uses the following expressions: Arithmetic- evaluates to a number (modulus, increment, decrement…), String- evaluates to a character string, for example "Fred" or "234", Logical- evaluates to true or false (and(&&), or(), not(!)) . When creating a variable you have to assign it a value or it is referred to as undefined and will produce a run time error. Using the keyword null can help assign a value to empty variables. JavaScript also implements a conditional operator which assigns one of two values according to a condition that has to be met or not met.

Wednesday, February 01, 2006

JavaScript Data Information

“A weakly typed language treats variables as generic containers which can hold any potential type of information. JavaScript is weakly typed because it treats functional objects as generic entities that can also hold multiple potential structures.” A dynamic language is one where the compiler does not need to know data types at compile time. The type can be modified whenever. JavaScript implements dynamic typing through the use of prototypes, which allows the possibility to alter both instance and class structures at runtime. Inheritance is accomplished by cloning existing objects which serve as prototypes for the new ones. In addition, JavaScript interpreters understand two different, basic types of data: numbers and character strings. Numbers in JavaScript are represented in binary as IEEE-754 Doubles. JavaScript also uses arrays as maps from integers to values. “Arrays have a length property that is guaranteed to always be larger than the largest integer index used in the array. It is automatically updated if one creates a property with an even larger index.” It also gets rid of the largest index if a smaller number is written to the array. Opposed to C that relies on standard I/O libraries, a JavaScript engine relies on a host environment in which it is embedded. For example, JavaScript embedded in a web browser connects through interfaces called Document Object Model (DOM) to applications. A major use of JavaScript is to write functions that are embedded in HTML pages that interact with the DOM of the page to perform actions that could not be possible through HTML alone. JavaScript is also embedded in other applications outside of the web like Adobe Acrobat, Mozilla, Microsoft’s Active Scripting etc… JavaScript gets access to the host environment in each one of these applications because they all provide their own object model that grants access.