HOME  |    TRAINING  |   FREE TUTORIALS   |   JOBS
Find out more about our new RSS feed.
FREE Tutorial
FUNCTIONS AND OBJECTS IN JAVASCRIPT

CATEGORY
SEARCH OUR OTHER TUTORIALS

DESCRIPTION

Functions and Objects are very important in JavaScript, within this tutorial the subject of functions and objects are explained.


TUTORIAL TAKEN FROM COURSE : JAVASCRIPT PROGRAMMING

FULL COURSE DETAILS

This training course aims to teach the reader the fundamentals of JavaScript. This course covers topics such as - variables and operators, control statements, functions, objects, frames, forms, dates, math and an introduction to cross-browser compatibility.

TO ACCESS THE FULL COURSE AND HUNDREDS OF OTHERS, CLICK HERE.


Functions

Functions are one of the fundamental building blocks in JavaScript. A function contains some code that will be executed by an event or a call to that function. Actually, function is a set of statements. You can reuse functions within the same script, or in other ones. You define functions at the beginning of a file (in the head section), and call them later.

Functions can have any number of parameters or none at all. You can create functions with changeable number of parameters that is not specified. This will be described in other parts of this course.

A function definition consists of the function keyword followed by

  • The name of the function.
  • A list of arguments to the function enclosed in parentheses and separated by commas.
  • The JavaScript statements that define the function, enclosed in curly braces, { }. The statements in a function can include calls to other functions defined in the current application.

function basicFunction () {
// function body ...
}

function basicFunction ( someValue ) {
// function body ...
}

The return statement specifies the value returned by the function.

function basicFunction (a,b) {
// function body ...

return (a + b)
}

All parameters are passed to functions by value; the value is passed to the function, but if the function changes the value of the parameter, this change is not reflected globally or in the calling function.

A method is a function associated with an object. You'll learn more about objects in next topic.

Objects

Being a scripting language, JavaScript is also an object-oriented one to some extent. It implies that JavaScript has two types of objects: predefined objects and user objects. The first are "ready-to-use" ones, which can only be observed and changed by user. For example, such ojects as Window, Frame, Location, History, Navigator, document, image, form, etc. cannot be created by user. On the contrary, it is users that create user objects.

To understand what JavaScript object is, consider it a container for properties and methods, as well as other objects. As you already know from the JavaScript Programming Overview section of Lesson 1, properties and methods are objects’ dominant characteristics, which you can access using the "." operator. Any oject can be accessed in the same way.

Example.

You have created the myObject object with properties par1, par2, and methods meth1() and meth2(). You can refer to them as:

myObject.par1 = myObject.par2 + 20;
myObject.meth1();.

Learning Object-Based Programming

You have already acquainted with JavaScript basics, such as definitions of objects and methods. Now let us have a deeper insight into object-based programming in general. Let us understand these important things:

  1. What we can get by programming in such languages as JavaScript.
  2. What their advantages are.
  3. How we can think in objects.

  1. First, we can create more effective code. The reason is that you do not have to know all about objects’ inner structure, since they are "ready-to-use" and can perfectly work without your awareness of how they were made. Besides, you can reuse the code, that is to use object models more than once. These important features make coding much easier and faster. You can concentrate on achieving your aims, not implementating.
  2. Actually, there are two types of programming: functional one where the code is a set of functions and their calls; and object-oriented one where the code is to create objects and their interaction. The code of the first type is hard to read and understand, while the code of the second type is easily read and supplemented.
  3. To think in object categories is quite natural for human beings. To create programs in object-based languages is not as difficult as you might think.

    Imagine the Clock object. It has two parameters at least: an hour hand and a minute hand. Both of them move according to some, of which we might be unaware, but use their results, however. We can watch the time, alter the clock, etc. All clocks in the world can be considered a class of objects, while the clock on your desktop is an instance.

    Thus, in JavaScript as in an object-based programming language, we deal with objects to use their properties.

Using Functions

The best way to describe how and why a function is to show a simple one in action. Here's a basic function that displays "Hello, JavaScripters!".

function basicFunction () {
alert ("Hello JavaScripters!");
}

This merely defines the function. JavaScript will do nothing with it unless the function is referenced somewhere else in the script. You have to call the function in order to use it. Calling a user-defined function is the same as calling a built-in JavaScript function -- you merely provide the name of the function in your script. This serves as the function call. When JavaScript encounters the function call, it executes instructions of that function. When the function is over, JavaScript returns to the point immediately after the function call, and processes the remainder of the script.

The browser processes the contents of the <SCRIPT> tag as the document loads. When it encounters the basicFunction() function call, it momentarily pauses to process the function, and the alert box appears. Click OK and the remainder of the page will be loaded.

OOD PRACTICE Define all your functions in the HEAD of a page the functions are loaded first. a user loads the page.

The arguments of a function are not limited to strings and numbers. You can pass whole objects to a function, too.

Using the arguments Array

The arguments of a function are maintained in an array. Within a function, you can address the parameters passed to it as follows:

functionName.arguments[i]

where functionName is the name of the function and i is the ordinal number of the argument, starting at zero.

So you can call the function with more arguments than it has in definition. Use this technique if you do not know exactly how many arguments your function will receive. To determine of how many arguments was received, use the argument.length property.


NEXT PAGE



5 RELATED COURSES AVAILABLE
JAVASCRIPT PROGRAMMING
This training course aims to teach the reader the fundamentals of JavaScript. This course covers topics such as -....
HTML 4.0 INTRODUCTION
To create, format and publish a small website using HTML 4.0. You will learn to create web pages incorporating fo....
INTRODUCTION TO JAVA PROGRAMMING
The aims of this Java training courses is to understand the role that Java plays on the Internet; describe the be....
MICROSOFT VISUAL BASIC V6 INTRODUCTION
To go from the fundamentals of Visual Basic programming to the threshold of Advanced level. Gaining in depth prog....
MICROSOFT VISUAL BASIC 5.0 CLIENT SERVER DEVELOPMENT
This course teaches the skills required to develop client server applications using MS Visual Basic 5.0 Enterpris....
 
0 RELATED JOBS AVAILABLE
CONTACT US
Friday 5th September 2008  © COPYRIGHT 2008 - VISUALSOFT