Please navigate to the bottom of the page for Table of Contents

Sunday, July 29, 2012

JavaScript functions explained

Functions are the basic building blocks in JavaScript. They can be used everywhere, can be passed as parameters, attached as event handlers, overridden, deleted, etc. Functions can be anonymous or not.

// In the example below, dont forget the semicolon!
// However, its name will not appear in the debugger
var anonymous = function() {
console.log('anonymous function!');
};

// This the classic syntax, without a semicolon at the end!
function nonAnonymous() {
console.log('non anonymous!');
}

// This is similar to the above, but is more
// debugger-friendly, as the name of the function
// will be printed in the debugger.
var nonAnonymous2 = function nonAnonymous2() {
console.log('non anonymous too!');
};

// This is the syntax for adding event handlers in jQuery
$('field').bind('tap',
function(event, data) {
console.log('binding an anonymous function!');
event.preventDefault();
});

2 comments:

  1. Nice Explained JavaScript functions, Helful me & also given answer 7 question on www.tutorialsolution.com - Thanks sir

    ReplyDelete