Basic data types (strings, integers, ...) are passed by value whereas objects are passed by reference.
function function-name(arg1,arg2,arg3) {
===Functions as objects and anonymous functions===
statements;
Functions are [[first-class object]]s in JavaScript. Every function is an instance of <code>Function</code>, 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 function]]s. For example:
return expression;
}
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 <code>Function</code> 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];
Since JavaScript 1.3 every function has the methods ''apply'' and ''call''
which allows the use of this function in the context of another object.<br>
The statements ''F.apply ( myObj , [ a1 , a2 , ... , aN ] )''<br>
and ''F.call ( myObj , a1 , a2 , ... , aN )''<br>
performs ''F ( a1 , a2 , ... , aN )'' as if this function would be a method of the object myObj.
function sum () { var s = 0; for ( var i = 0 ; i < arguments.length ; i++ ) s += arguments [i] ; return s }
var dummy
alert ( sum.apply ( dummy , [ 1 , 2 , 3 ] ) ) // == 1 + 2 + 3 == 6
alert ( Math.pow.call ( dummy , 5 , 2) ) // == 5 × 5 == 25
''Apply'' and ''call are mainly used to perform inheritance, see below.
==Objects==
|