Make your XPages more maintainable – JavaScript Callback functions


Xomino

In this article I will attempt to explain the purpose and benefits of using callback functions in JavaScript.

Introduction

Basic JavaScript functions look like this normally like this

JavaScript variables normally look like this

Finally we would call the function to add the variables

Seems simple enough

Setting a variable to a function

But we can also combine them into something like this

The point of this simple demo is to illustrate that a variable can be “equal” to a function. So that means we can pass it around as a function.

Creating a callback function

Here is an example:

Which is called like this

Which returns 3

Walking through that…

  1. We created theTotal variable and made it equal to a function call to addMe
  2. We called the addMe function passing 1, 2 and a function with a value as a parameter
  3. The addMe function received the variables and assigned the name…

View original post 567 more words