Wednesday, September 18, 2013

jQuery background

Before starting learning jQuery, it's helpful to know some following javascript tricks,

Anonymous Functionsg

  • Anonymous functions are nameless function (function having no name).
  •  Anonymous functions are executed as soon as they are downloaded by browser (if they are not within another function).
  • They are in 2 types.
 
 First type, 
(function(){
  //some cool js stuff here.
});
 
Second type (with passing arguments),
(function(args){
  //some cool js stuff here.
})(args); 
//Above last line will pass args. This looks odd having args 2 times. But learning this way is helpful.
 
Eg. 
(function(args){
  alert(args);
})("hi"); 
 Above function will alert hi
 
Other interesting stuff of anonymous functions in javascript,
 
We can pass anonymous function as argument, like we pass int, string, etc.
Eg,
 
function acceptAnonymousFunction(annonymousFunc) {
    // some code here
    annonymousFunc('javascript!');
}

acceptAnonymousFunction(function(arg) {
    alert('Hello '+ arg);
}); 
// alerts 'Hello javascript!'  
Got it! If not re-read it carefully, you will understand it. 
 
Assigning javascript function as variable,
var myfunc = function(args) 
{                     
  //functionality of myfunc here
}
myfunc(args); // this is calling of myfunc
 
Above code will do exactly same as,
function myfunc(args){
  //functionality of myfunc goes here
} 

More interesting stuff...coming soon!!Keep reading.
 

No comments:

Post a Comment