NOTE
THIS MODULE IS STILL A DRAFT.
In JavaScript functions may be defined as in other languages like C and PhP.
function square(x) = {return x * x}However in addition in JavaScript functions are objects. They may be defined as anonymous functions.
Definition
Wikipedia: In programming language theory, an anonymous function is a function (or a subroutine) defined, and possibly called, without being bound to an identifier.
This means that the function expression does not need an identifier or name. In JavaScript an object is created with the function expression.
Form
An anonymous function looks like this:
function() { // statements } or function(x) { // statements }
An example of an anonymous function expression function(x) {return x * x} . Compare this to above where we had given square as the function identifier.
Assigning the function expression to a variable
The anonymous function expression may be assigned to a variable.
var square = function (x) { return x * x}
Test
square(8) gives back 64. The function object may be referenced by another variable var sq = square; . Note that there are no invocation () brackets. sq(9) gives back 81.
Method definition with an anonymous function
A method is a property whose value is a function.
var myObj = {};
myObj.lengthOfSide = 9;
myObj.area = function() {return this.lengthOfSide * this.lengthOfSide};
With var myObj = {} a new object is created. With myObj.lengthOfSide = 9; a data property is assigned to it. And with myObj.area = function() {return this.lengthOfSide * this.lengthOfSide}; a method is assigned.
console.log(myObj.area()) puts 81 to the log.
console.log(myObj.area) gives back the function object(function () {return this.lengthOfSide * this.lengthOfSide;}).



HTML5 template
