Java Script

Only available on StudyMode
  • Topic: Closure, Recursion, C
  • Pages : 7 (1085 words )
  • Download(s) : 105
  • Published : January 10, 2013
Open Document
Text Preview
Java Script
Author By Teacher(Vijaya)
Chapter-7

Function Expressions

There are two ways to define a function: by
1.function declaration
2. function expression.

Function Declaration:

function functionName(arg0, arg1, arg2) {
//function body
}

> name property on functions exposing the assigned name.

Ex: alert(functionName.name); //”functionName”

> key characteristics of function declarations is function declaration hoisting, whereby function declarations are read before the code executes.

Ex:

sayHi();
function sayHi(){
alert(“Hi!”);
}

This example doesn’t throw an error because the function declaration is read fi rst before the code begins to execute.

Function Expression:

var functionName = function(arg0, arg1, arg2){
//function body
};

The created function is considered to be an anonymous function, because it has no identifier after the function keyword. (Anonymous functions are also sometimes called lambda functions.) This means the name property is the empty string.

Ex:
sayHi();
//error – function doesn’t exist yet
var sayHi = function(){
alert(“Hi!”);
};

Function Declaration | Function Expression |
//never do this! if(condition){ function sayHi(){ alert(“Hi!”); } } else { function sayHi(){ alert(“Yo!”); } } | //this is okay var sayHi; if(condition){ sayHi = function(){ alert(“Hi!”); }; } else { sayHi = function(){ alert(“Yo!”); }; } |

Recursion:
A recursive function typically is formed when a function calls itself by name .

Ex:
function factorial(num){
if (num <= 1){
return 1;
} else {
return num * factorial(num-1);
}
}

Although this works initially, it’s possible to prevent it from functioning by running the following code immediately after it:

var anotherFactorial = factorial;
factorial = null;
alert(anotherFactorial(4)); //error!

because it will try to execute factorial(), which is no longer a function.

arguments.callee

arguments.callee is a pointer to the function being executed and, as such, can be used to call the function recursively.

Ex:
function factorial(num){
if (num <= 1){
return 1;
} else {
return num * arguments.callee(num-1);
}
}
It’s advisable to always use arguments.callee of the function name whenever we’re writing recursive functions . Closures:

Closures are functions that have access to variables from another function’s scope.

Ex:
function createComparisonFunction(propertyName) {
return function(object1, object2){
var value1 = object1[propertyName];
var value2 = object2[propertyName];
if (value1 < value2){
return -1;
} else if (value1 > value2){
return 1;
} else {
return 0;
}
};
}
Stack Frames:

function sayHello($name) {
$return = 'Hey there' +$name;
return $return;
}
sayHello('Peter'); //echos 'Hey there Peter'.
The above function when run will create a stack-frame for itself, and $return will exist and be available inside that stack frame. Once sayHello() is done $return vanishes into the garbage collector, and is gone until the next time sayHello() is invoked.

In Javascript we are able to declare inner and anonymous functions. These functions are enclosed by their containing function and are said to form a closure.

Executable Code and Execution Contexts:

When control is transferred to ECMAScript executable code, control is entering an execution context. It is possible to enter a new execution context from current execution context, these contexts logically form a stack.

Execution context is created dynamically when program is running.

For instance, function creates a new execution context when it is invoked. Repeatedly invoking functions (including recursion) will repeatedly create new execution contexts. Logical stack changing on program running when new function invoked, function returned,...
tracking img