Friday, June 3, 2011

Javascript Function Objects

Having:

function validateFields() {
// a lot of cool and interesting code goes here.
var a = 1;
var b = 2;
var c = a + b;
}

the following

$('#submit').bind('click', function() {
validateFields();
});

is similar to this:

$('#submit').bind('click', validateFields);

and, to this as well:

$('#submit').bind('click', function() {
var a = 1;
var b = 2;
var c = a + b;
});

So you could say that

function validateFields() {
// a lot of cool and interesting code goes here.
var a = 1;
var b = 2;
var c = a + b;
}

is essentially the same as

var validateFields = function() {
// a lot of cool and interesting code goes here.
var a = 1;
var b = 2;
var c = a + b;
}

i.e. validateFields is not a function/method but an object. Therefore I conclude "Javascript is a different language in disguise."

No comments: