Wednesday, June 27, 2012

JavaScript Functions - Part 2

In the first part I tried to cover some basics about JavaScript functions. In this I am scribbling some more about JavaScript functions. 

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 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
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
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 Functions
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
  • 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. 

Tuesday, June 12, 2012

JavaScript Functions - Part 1


A function is a block of code which will execute whenever it is called. To become a good JavaScript programmer understanding functions are the key.

In JavaScript functions are declared with function keyword. It is possible to create named functions, anonymous functions / lambda functions, self invoking functions. You can pass parameters to functions; these parameters can be any JavaScript type.

// named function
function myfun(){
document.write(“this is simple named function”);
}
// named function with params
function myfun (name,age){
document.write(“My name is “  +  name,  “ I am “  +  age);
} 

To call a function we have to use () operator. In the above example myfun is the function name so you can call function like below.

//invoking a named function
myfun(); 

All the declared parameters are local variables to the functions. You can omit declared parameters; misplace the order of parameters, mismatch data types when calling a function, it is perfectly legal in JavaScript.

//ignored age param
myfun("srinivas"); // prints srinivas
//params are local varibles to the function
function myfun(name,age){
name = "srinivas";
age = 23;
document.write(name," " + age);
}
myfun("something", 25); // prints srinivas 23
Functions are objects in the JavaScript. so you can pass them as parameters and can assign to the variable.

// creating a anonymous function and assigning them to a variable
var printNameAge = function(name,age){
document.write(name," " + age);
}
// whenever you need to call this anonymous function need to call like below
printNameAge('srinivas',23); // prints srinivas 23

Variables / parameters declared in the functions are private variables. If you want to make them as public use ‘this’ keyword.

//using the this key word
function myfun(name,age){
this.name = "srinivas";
this.age = 23;
document.write(name," " + age);
} 
myfun("something", 25); // prints something 25

Self invoking function is a function which will invoke itself. by using these we can create some utility functions and modules. These are very helpful to make the global space clean and avoid name space collisions.

// simple self invoking function
(function(){alert('hi')})();

Summary
  1. Functions are declared with the function key word, every function has a body with some statements.
  2. Functions will be executed with the () operators.
  3. Functions may or may not have parameters
  4. It is perfectly legal omitting some of the parameters, passing them in a different order, passing different types.
  5. Functions can be named functions, anonymous functions, self invoked functions
  6. Function parameters are local variables to the functions
  7.  By default declared variables in the function are private, to make them as public use ‘this’ keyword.






Sharing something about web development

After receiving lot of questions from my juniors, I have decided to share my knowledge by posting something on mobile development with HTML5, JQUERY Mobile and PhoneGap (HTML5 App platform that allows you to author native applications with web technologies and get access to APIs and app stores – developed by nitobi, now this company acquired by Adobe).  So many web designers, developers are not aware of lot of buzz in the recent web, mobile development.

  1. HTML5
  2. JQURY Mobile
  3. Sencha Touch
  4. Node
  5. EXT JS 4.x
  6. Object Oriented JavaScript
  7. CSS3
  8. Phonegap
above are most popular. they are looking  promising solutions for the current application needs. i will try to post simple and easy step by step process to learn above. hope my posts will useful to some developers at beginner level.