In Java Script you can code a function at design time, or you can have Java Script write the function code during execution. A function that is created at runtime is called an "anonymous" or "dynamic" function.
Welcome to Bucaro TecHelp!

Bucaro TecHelp
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact RSS News Feeds News Feeds

Victims of Sandy Hook

Stop the Slaughter of Innocents. Congress is bought and paid for by gun lunatics and gun promotion groups. If you want to live in a safe America, help buy Congress back for America. Send a donation to Mayors Against Illegal Guns, 909 Third Avenue, 15th Floor New York, NY 10022

Creating Java Script Dynamic or anonymous Functions at Runtime

You may recall that a function is a block of code that performs a specific task. Sometimes a function returns a value. In Java Script you code a function by using the keyword function, followed by the function's name, followed by a list of parameters that get passed to the function (enclosed in parenthesis), followed by the block of code that performs the task (enclosed in curly braces).

Another way to create a function is to have Java Script write the function code during execution. Why would a program need to create a function at runtime? This could be used in a program that allows the user to add functionality to the application during execution.

A function that is created at runtime is called a dynamic function. To create a dynamic function, use the keyword new, followed by the keyword Function (Note use of upper-case F in keyword Function when creating dynamic functions), followed by a list of parameter values enclosed in quotes and separated by commas ("parameter1","parameter2","parameter3","statements"), the final entry in the list being the statements that will execute when the function is called.

Shown below is an example of code for a dynamic function:

<script type="text/javascript">

var calcVolume = new Function("height","width","depth","var vol = height * width * depth; return vol;");

var result = calcVolume(10,4,6);


The first line after the script block opening tag above is the declaration of the dynamic function, placing a reference to it in a variable named calcVolume. The second line calls the dynamic function using that variable name, placing the return value in a variable named result. The third line uses the alert function to display the value in result.

Dynamic functions are sometimes called "anonymous functions" because, as you can see in the code above, technically the function doesn't have a name. In this example, the name calcVolume is the name of the variable that contains a reference or pointer to the function in the computer's memory.

Another note about anonymous functions is, because their reference is contained in a variable, their scope is the same as any variable declared at that same level. In other words, if the code to create an anonymous function is within a function, that anonymous function disappears upon exit of the function within which it was created.

RSS Feed RSS Feed

Follow Stephen Bucaro Follow @Stephen Bucaro

Web Design Sections

Fire HD
[Site User Agreement] [Privacy Policy] [Site map] [Search This Site] [Contact Form]
Copyright©2001-2016 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268