Sun Dial Menu

JavaScript Functions

The more advanced code that I have been looking at requires a better understanding of JavaScript functions, how they are used to create prototype objects and how these objects are used within the Document Object Model of a webpage. Many of the techniques to enable Accessibilty and more complex page behaviours, such as expandable accordians and other interactive screen devices (modals) are achieved this way.

The Propagation of events and parameter useCapture in the anonymous function.

There are far too many JavaScript functions for me to document and I have no intention of trying! I only look into function as I find them in code that I am using.

The example below calls a JavaScript function when the paragraph with the id="demo" is rendered. It is not easy to see below but the contents of the paragraph is instantiated with the value of 4 times 3.

Function Declarations

Declared functions are not executed immediately. They are "saved for later use", and will be executed later, when they are invoked (called upon). It is generally the case that frequently called functions will be placed in a seperate .js file and linked to the pages that call those functions.

A Script object can also be defined in an HTML file using the <script> .... </script> tags.

The example below calls a function (when the page loads) which performs a calculation and returns the result:

This is the code:

var x = myFunction(4, 3);
document.getElementById("demo").innerHTML = x;

function myFunction(a, b) {
  return a * b;

The result is:

A JavaScript Function can also be made "inline"

Anonymous Function

An anonymous function is a function without a name. An anonymous function is often not accessible after its initial creation.

Top Menu



Top Menu

Site design by Tempusfugit Web Design -