Monday, June 10, 2013

Functions in Javascript

Functions



Functions not a tool in javascript they are "The Tool" in javascript. For all the functionality that other object oriented languages have that javascript does not, functions can implement, simulate and to some extent bend the rules to provide the equivalent to that of traditional languages.

What Can I do with a Function?

The better question is what can't you do with them.
Functions can be 
  • Passed as arguments
  • Returned as values
  • Assigned to a variable
  • Stored in a data structure
  • Nested (Closures more on this later)
  • Anonymous (Lambda Expressions
Additional just about all functions in javascript can be overwritten (including the built in ones) so if you come from a language with Method Overriding you can think of all functions in javascript as being virtual. Thinking about that for a second... you have the power to change just about anything...

Uncle Ben likes functions and so should you.
Uncle Ben




As uncle Ben once said with great power comes great responsibility. Which is exactly why using things like closures to scope functions is important as I will show you a bit about later.







Function Declarations

So we have three ways to declare functions
the first of which is called the functional statement. Key thing to remember about the functional statement is that by default it is declared in the global scope (which is usually the parent document window in most cases)



Just what the heck is a Closure?

A closure is any function which closes over the environment in which it was defined. This means that it can access variables not in its parameter list. Closures are the bread and butter of javascript and are the means by which javascript can emulate other languages object oriented nature.



Anonymous

Anonymous, you mean like a Jon Doe or like that Hacker group? Or like the horse with no name?

Those masks really freak me out.
A horse with no name....?





Well, if I had to pick I'd say they are somewhat closer to the horse.... maybe






Anonymous methods are methods that do not have a method named attached to them. In particular to javascript they are often used as a means to manage the scope of a object , module, or namespace as such. In this quick jQuery example we pass the button an anonymous function that declares a variable. The function only gets called when the button is clicked and only lives as long as the lifetime of the button click itself.






Also often used to act as events within another function.

A common example you'd commonly see out in the wild would be the jQuey Ajax Request function





*Note I use jQuery with both because jQuery is pretty close to synonymous with javascript on many a website and felt it to be more realistic and relevant to the discussion.



Well that about wraps up my brain dump about functions. This post is (for the most part) an adaptation of my talk on javascript fundamentals. If this stuff is making your brain itch

then you should look out for my next javascript presentation.