| Functions and methods | | Print | |
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.
| Users' Comments (0) |
|
No comment posted






