Online
 
Sunday, 23 November 2008
 
 

Functions and methods | Print |  E-Mail
 

Functions and methods

A function is a piece of JavaScript code that is defined once and can be executed multiple times by a program. A function definition looks like this:

function sum(x, y) {



return x + y;



}

Functions are invoked using the ( ) operator and passing a list of argument values:

var total = sum(1,2);  // Total is now 3

In JavaScript 1.1, you can create functions using the Function( ) constructor:

var sum = new Function("x", "y", "return x+y;");

In JavaScript 1.2 and later, you can define functions using function literal syntax, which makes the Function( ) constructor obsolete:

var sum = function(x,y) { return x+y; }

When a function is assigned to a property of an object, it is called a method of that object. Within the body of a method, the keyword this refers to the object for which the function is a property.

Within the body of a function, the arguments[ ] array contains the complete set of arguments passed to the function. See Function and Arguments in the reference section.

 

This entry was posted on . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a comment. Tags: javascript, java script, html, free code, window, menu, form, popup, source, date calculator, calendar, javascripts, image, javascript help, javascript popup window, forms, calendars, games, Web design builder, webmaster, development.
Users' Comments (0)

Comment an article
  Name
  E-mail
   Title
Available characters: 4000
 Notify me of follow-up comments
This image contains a scrambled text, it is using a combination of colors, font size, background, angle in order to disallow computer to automate reading. You will have to reproduce it to post on my homepage
Enter what you see:

No comment posted

Jumbo Coklat
 
Top! Top!