Function with return statement
functions may return some value or they can be void (doesn't return anything). that return value can be any JavaScript type(remember functions are objects in the JavaScript-so you can return a function with in the function).
//function with a return statement
//function with a return statement
function sum (val1,val2){
return (val1+val2);
}
summedValue = sum(2,3);
alert(summedValue) //alerts 5
//returning a function
function myfunc(){
return alert('hi');
}
myfunc() // alerts hi
//returning anonymous function
function myfunc(){
return function(){alert('hi')};
}
// storing as data
var myHello = myfunc;
alert(myHello);// alerts function myfunc() {return function () {alert("hi");};}
when ever you are assigning the myfunc() to variable you are storing the anonymous function reference to that variable.
// executing the functions and storing
var myHello = myfunc();
myHello();//alerts hi
you can execute above anonymous inner function without assigning to another variable by keeping ()().
myfunc()()//alerts hi
Function may be nested
//returning a function
function myfunc(){
return alert('hi');
}
myfunc() // alerts hi
//returning anonymous function
function myfunc(){
return function(){alert('hi')};
}
// storing as data
var myHello = myfunc;
alert(myHello);// alerts function myfunc() {return function () {alert("hi");};}
when ever you are assigning the myfunc() to variable you are storing the anonymous function reference to that variable.
// executing the functions and storing
var myHello = myfunc();
myHello();//alerts hi
you can execute above anonymous inner function without assigning to another variable by keeping ()().
myfunc()()//alerts hi
Function may be nested
You can write n number of functions inside a function. Those inside functions are private. You can make them as public with THIS key word. Variables, arguments of the outer function can be accessed through inside function. (There is no block level variable scope in JavaScript).
//nested function
function someValue(vala,valb){
function multiplySame(val){
return val * val;
}
return multiplySame(vala) * multiplySame(valb)
}
someValue(10,20);//prints
40000
//accessing variable form inside function
function myfun(){
var myvar =1;
function addVar(){
var somevar = 2;
return somevar + myvar;
}
return addVar();
}
alert(myfun());// alerts 3
In some scenarios we need to initialize some values during the first execution. blow function is an example for your reference
var saySomething = function(name){
var message;
function initMessage(){
message = 'hi';
return message;
}
function sayMessage(){
alert(message + name);
}
initMessage();
return sayMessage;
}
saySomething('srinivas')// alerts hisrinivas
in JavaScript functions are objects. so it is perfectly legal to pass a function as param to another function. when you are passing a function to another function that passed function is executed in the function those functions are called callback functions. they are very useful during while communicating with server and fetching some data form remote locations.
function simpleCallback(name,age,callback){
alert("normal function with call bcak " + name +" " + age);
callback();
}
simpleCallback('srinivas',34,function(){
alert('from callback')
});
Summery
//accessing variable form inside function
function myfun(){
var myvar =1;
function addVar(){
var somevar = 2;
return somevar + myvar;
}
return addVar();
}
alert(myfun());// alerts 3
In some scenarios we need to initialize some values during the first execution. blow function is an example for your reference
var saySomething = function(name){
var message;
function initMessage(){
message = 'hi';
return message;
}
function sayMessage(){
alert(message + name);
}
initMessage();
return sayMessage;
}
saySomething('srinivas')// alerts hisrinivas
var message;
function initMessage(){
message = 'hi';
return message;
}
function sayMessage(){
alert(message + name);
}
initMessage();
return sayMessage();
}
saySomething('srinivas')
function myfun(){
var myvar =1;
function addVar(){
var somevar = 2;
return somevar + myvar;
}
return addVar();
}
alert(myfun());
Callback Functionsin JavaScript functions are objects. so it is perfectly legal to pass a function as param to another function. when you are passing a function to another function that passed function is executed in the function those functions are called callback functions. they are very useful during while communicating with server and fetching some data form remote locations.
function simpleCallback(name,age,callback){
alert("normal function with call bcak " + name +" " + age);
callback();
}
simpleCallback('srinivas',34,function(){
alert('from callback')
});
Summery
- Functions can return some value that may be any JavaScript type
- Functions can be nested
- Inner functions can access the outer function variables and parameters.
- Functions may return functions
- Functions can be passed as parameters, if passed function is executed in the called function. Then that passed function is called callback function.
No comments:
Post a Comment