Monday, March 06, 2006

Object-based vs. object oriented and built-in functions

Continuing on with my quest to master the JavaScript language I am going to find out whether or not “object-based”(JavaScript) is different from object-oriented and if so, how? Also, I will discuss what built-in functions are available for JavaScript and tell you which I find the most useful and why.

When searched on Wikipedia “Object-based” programming is defined as “a somehow limited version of object-oriented programming where one or more of the following restrictions applies: there is no implicit inheritance, no polymorphism, and only a very reduced subset of the available values are objects, typically the GUI components.”
Objects in object-based languages are complete packages; everything that describes the implementation of the object is self-contained. One object doesn't share its implementation with any other objects. There is also no way to group a set of objects as a generic type. The ability to group a set of classes under a generic class is at the root of object oriented programming (polymorphism).

In addition, object-based is also referred to as prototype-based. According to Wikipedia, “Prototype-based programming is a style and subset of object-oriented programming in which classes are not present, and behaviour reuse (known as inheritance in class-based languages) is accomplished through a process of cloning existing objects which serve as prototypes.”

Now on to built-in functions…

Some top-level functions that are built-in to JavaScript are eval, parseInt, and parseFloat. The eval function takes a string as its argument and is useful for evaluating a string representing an arithmetic expression. Its argument is not limited to just evaluating numerical expressions. Its argument can include object references or even JavaScript statements. parseInt and parseFloat return a numeric value when given a string as an argument. paseInt attempts to return an int and parseFloat attempts to return a floating-point number. If either function is unsuccessful, then it returns “NaN” (not a number). If they encounter a character that is not a numeral in the specified radix, they ignore it and truncate the remaining characters, returning the converted digits.

JavaScript also has three built-in objects and can be used in either client or server scripts: string, Math, and Date. For more information of the functions that each object incorporates check out:
http://docsrv.sco.com/INT_netscapeJava/builtin.htm

The function that I find the most useful depends obviously on what the programmer is trying to accomplish but I like the eval function because of its ability to accept and evaluate arguments of different data types.

Function and procedure structure/Parameters

The purpose of this week’s blog is to discuss how procedures and functions are structured in JavaScript. They are essentially the same thing and are a fundamental building block of most JavaScript programs. A function groups together (using curly braces) a set of statements under a subroutine named by the programmer. At any time in the program that you need to perform the task that this subroutine accomplishes all you have to do is “call” the function. However, first you must create the function and an ordinary function is structured like this:

function_name(parameters) {

JavaScript commands

}

The functions are placed in the HTML file in the SCRIPT tags before the function call. JavaScript allows you to create as many of these subroutines as you need in your program along with all the subroutines it offers in its standard library to complete your objectives.
If you need to reuse a function in another program you can just copy and paste it from your previous code.

The parameters in between the paranthesis above are what the program uses, or needs, to complete the task it is designed for. You may pass as many parameters as necessary to complete the job. If you do pass multiple parameters you must make sure that each variable type matches in the function declaration as in the function call (like if the function is expecting an int, int, float you have to pass variables of those data types in that specific order in the call otherwise an error occurs). Basically, there is nothing too significantly different from Java or C++ when it comes to parameter passing in JavaScript. The end.

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.

Monday, January 30, 2006

Java

Who: Sun Microsystems (Key people: Patrick Naughton, Mike Sheridan, James Gosling, and John Gage -- Director of the Science Office)
What: Originally designed for programming household devices, Java quickly evolved with the World Wide Web, and was re-focused as the language for the internet. It uses a comprehensive standard library (Java Application Programming Interface) at its center. Also, a set of library classes provide an easy way to create graphical user interfaces. It allows a user to create a program with predefined components instead of starting from scratch. Although Java is a simplification of C++, it is a purely object-oriented language.
“Since its introduction in May 1995, the Java platform has been adopted more quickly across the industry than any other new technology in computing history. All major computing platform vendors have signed up to integrate Java technology as a core component of their products.” ( http://java.sun.com/features/1998/05/birthday.html )
When: Project started in 1990-91, originally named Oak. Renamed Java in 1995.
Where: Palo Alto, California
Why: According to Gosling, "The goal was ... to build a system that would let us do a large, distributed, heterogeneous network of consumer electronic devices all talking to each other” (http://ei.cs.vt.edu/~wwwbtb/book/chap1/java_hist.html ). The World Wide Web was growing and Sun was looking for a target market. After trying the cable companies and failing, they asked themselves why not the internet? So they adapted their Java technology into a language for the Internet. It created the idea of applets which function as a way to animate web pages. It is successful because many users that originally new C++ can learn Java very easily and it makes use of extensive libraries to program efficiently. Java simplifies the use of objects.

Monday, January 23, 2006

Three Historical Links

http://plato.stanford.edu/entries/computing-history/
This site covers the history of computing machines beginning with Babbage. Babbage proposed a “Difference Engine that was a special-purpose digital computing machine for the automatic production of mathematical tables.” He later designed an Analytical Engine which was intended to be a mechanical digital computer. The programming language ADA is named after Ada Lovelace, who worked closely with Babbage. After Babbage, The site continues through the history of early computing.

http://inventors.about.com/library/blcoindex.htm?once=true&
This site provides a timeline of some of the major milestones in computer history, focusing mostly on PCs.

http://www.maxmon.com/history.htm
This site is the most thorough and interesting one I found. It takes us on a virtual tour through history beginning in the year 350 Million Years BC and ending in 1997 AD. It covers the “development of numbers… the evolution of electronics and the impact of electronics on computing.” Although no one person can take credit for the computer, the site gives credit where it’s due to the more “notable developments and individuals.”