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.