Javascript::Javascript-Functions
Functions
A function is a block with a (possibly empty) argument list that is normally given a name. A function may give back a return value.
function function-name(arg1, arg2, arg3) {
statements;
return expression;
}
Example: Euclid's original algorithm of finding the greatest common divisor. (This is a geometrical solution which subtracts the shorter segment from the longer):
function gcd(segmentA, segmentB) {
while (segmentA != segmentB) {
if (segmentA > segmentB)
segmentA -= segmentB;
else
segmentB -= segmentA;
}
return segmentA;
}
The number of arguments given when calling a function may not necessarily correspond to the number of arguments in the function definition; a named argument in the definition that does not have a matching argument in the call will have the value undefined
. Within the function the arguments may also be accessed through the arguments
list (which is an object); this provides access to all arguments using indices (e.g. arguments[0], arguments[1], ... arguments[n]
), including those beyond the number of named arguments.
Basic data types (strings, integers, ...) are passed by value wheras objects are passed by reference.
Functions as objects and anonymous functions
Functions are first-class objects in JavaScript. Every function is an instance of Function
, a type of base object. Functions can be created and assigned like any other objects, and passed as arguments to other functions. Thus JavaScript supports higher-order functions. For example:
Array.prototype.fold = function (value, functor) {
var result = value;
for (var i = 0; i < this.length; i++) {
result = functor(result, this[i]);
}
return result;
}
var sum = [1,2,3,4,5,6,7,8,9,10].fold(0, function (a, b) { return a + b })
results in the value:
55
Since Function
can be instantiated, JavaScript allows the creation of anonymous functions, which can also be created using function
, e.g.:
new Function( "return 1;" )
function() { return 1; }
The implicit object available within the function is the receiver object. In the example below, the value of the alert
property is an anonymous function:
function Point( x, y ) {
this.x = x;
this.y = y;
}
Point.prototype.alert = function() {
window.alert( "(" + this.x + "," + this.y + ")" );
}
var pt = new Point( 1, 0 );
pt.alert();
Methods can also be added within the constructor:
function Point( x, y ) {
this.x = x;
this.y = y;
this.alert = function() {
window.alert( "(" + this.x + "," + this.y + ")" );
}
}
var pt = new Point( 1, 0 );
pt.alert();
There is no need to use a constructor if only a single instance of an object is required and no private members are needed - properties and values can be added directly using an initialiser:
var pt = {
x: 1,
y: 0,
alert: function() {
window.alert( "(" + this.x + "," + this.y + ")" );
}
}
pt.alert();
Members declared as variables in the constructor are private; members assigned to this
are public. Methods added or declared in the constructor have access to all private members; public methods added outside the constructor don't.
function myClass() {
var msg = "Hello world!";
/*
This is shorthand for:
var myPrivateMember = function()
*/
function myPrivateMember() {
alert(msg);
}
this.myPublicMember = function() {
myPrivateMember();
}
}
myObj = new myClass;
myObj.anotherPublicMember = function() {
/*
These won't work as anotherPublicMember
is not declared in the constructor:
myPrivateMember();
alert(msg);
These won't work either:
this.myPrivateMember();
alert(this.msg);
*/
this.myPublicMember();
}
Note:
The functions '__defineSetter__' and '__defineGetter__' are implementation-specific and not part of the ECMAScript standard.
For detailed control of member access, getters
and setters
can be used (e.g. to create a read only property or a property that the value is generated):
function Point( x, y ) {
this.x = x;
this.y = y;
}
Point.prototype.__defineGetter__(
"dimensions",
function() { return [this.x, this.y]; }
);
Point.prototype.__defineSetter__(
"dimensions",
function( dimensions ) { this.x = dimensions[0]; this.y = dimensions[1]; }
);
var pt = new Point( 1, 0 );
window.alert( pt.dimensions.length );
pt.dimensions = [2,3];
..
Javascript::Javascript-Functions Related Sites
[READ MORE : Javascript::Javascript-Functions] |