PencilCoder

Anonymous Functions!

Goals:

  • Make use of anonymous functions for event handlers
  • Further explore jQuery methods plan, done, and each.

Coding Snippets:

  • coding snippets
  • The jQuery each method facilitates iteration over the elements of a jQuery collection. This higher-order function accepts a callback, which in this example is defined anonymously. The callback accepts two arguments, representing the index of iteration and the current element.

Math and Computer Concepts:

  • An anonymous function is defined without assigning it a name. We simply put the function definition where we normally would put a reference to that function.
  • One reason to use anonymous functions is convenience. They require less typing and their use often simplifies our code, such as when defining a callback that is used only once.
  • An additional benefit to anonymous functions is that their use reduces chances for namespace collisions: we don't have to worry that the name chosen for the function is a variable name already used elsewhere in our program.

Activities:

Save each of the following in a folder called AnonymousFcns.