Function Object

  • Revision slug: JavaScript/Guide/Obsolete_Pages/Predefined_Core_Objects/Function_Object
  • Revision title: Function Object
  • Revision id: 117768
  • Created:
  • Creator: Mgjbot
  • Is current revision? No
  • Comment robot Adding: ja

Revision Content

Function Object

The predefined Function object specifies a string of JavaScript code to be compiled as a function.

To create a Function object:

functionObjectName = new Function ([arg1, arg2, ... argn], functionBody)

functionObjectName is the name of a variable or a property of an existing object. It can also be an object followed by a lowercase event handler name, such as window.onerror.

arg1, arg2, ... argn are arguments to be used by the function as formal argument names. Each must be a string that corresponds to a valid JavaScript identifier; for example "x" or "theForm".

functionBody is a string specifying the JavaScript code to be compiled as the function body.

Function objects are evaluated each time they are used. This is less efficient than declaring a function and calling it within your code, because declared functions are compiled.

In addition to defining functions as described here, you can also use the function statement and the function expression. See the Core JavaScript 1.5 Reference for more information.

The following code assigns a function to the variable setBGColor. This function sets the current document's background color.

var setBGColor = new Function("document.bgColor='antiquewhite'")

To call the Function object, you can specify the variable name as if it were a function. The following code executes the function specified by the setBGColor variable:

var colorChoice="antiquewhite"
if (colorChoice=="antiquewhite") {setBGColor()}

You can assign the function to an event handler in either of the following ways:

1. document.form1.colorButton.onclick=setBGColor
2. <INPUT NAME="colorButton" TYPE="button"
      VALUE="Change background color"
      onClick="setBGColor()">

Creating the variable setBGColor shown above is similar to declaring the following function:

function setBGColor() {
   document.bgColor='antiquewhite'
}

Assigning a function to a variable is similar to declaring a function, but there are differences:

  • When you assign a function to a variable using var setBGColor = new Function("..."), setBGColor is a variable for which the current value is a reference to the function created with new Function().
  • When you create a function using function setBGColor() {...}, setBGColor is not a variable, it is the name of a function.

You can nest a function within a function. The nested (inner) function is private to its containing (outer) function:

  • The inner function can be accessed only from statements in the outer function.
  • The inner function can use the arguments and variables of the outer function. The outer function cannot use the arguments and variables of the inner function.

{{template.PreviousNext("Core_JavaScript_1.5_Guide:Predefined_Core_Objects:Date_Object", "Core_JavaScript_1.5_Guide:Predefined_Core_Objects:Math_Object")}}

{{ wiki.languages( { "ja": "ja/Core_JavaScript_1.5_Guide/Predefined_Core_Objects/Function_Object", "pl": "pl/Przewodnik_po_j\u0119zyku_JavaScript_1.5/Obiekty_predefiniowane/Obiekt_function" } ) }}

Revision Source

<p>
</p>
<h3 name="Function_Object"> Function Object </h3>
<p>The predefined <code>Function</code> object specifies a string of JavaScript code to be compiled as a function.
</p><p>To create a <code>Function</code> object:
</p>
<pre>functionObjectName = new Function ([arg1, arg2, ... argn], functionBody)
</pre>
<p><code>functionObjectName</code> is the name of a variable or a property of an existing object. It can also be an object followed by a lowercase event handler name, such as <code>window.onerror</code>.
</p><p><code>arg1</code>, <code>arg2</code>, ... <code>argn</code> are arguments to be used by the function as formal argument names. Each must be a string that corresponds to a valid JavaScript identifier; for example "x" or "theForm".
</p><p><code>functionBody</code> is a string specifying the JavaScript code to be compiled as the function body.
</p><p><code>Function</code> objects are evaluated each time they are used. This is less efficient than declaring a function and calling it within your code, because declared functions are compiled.
</p><p>In addition to defining functions as described here, you can also use the <code>function</code> statement and the function expression. See the <a href="en/Core_JavaScript_1.5_Reference">Core JavaScript 1.5 Reference</a> for more information.
</p><p>The following code assigns a function to the variable <code>setBGColor</code>. This function sets the current document's background color.
</p>
<pre>var setBGColor = new Function("document.bgColor='antiquewhite'")
</pre>
<p>To call the <code>Function</code> object, you can specify the variable name as if it were a function. The following code executes the function specified by the <code>setBGColor</code> variable:
</p>
<pre>var colorChoice="antiquewhite"
if (colorChoice=="antiquewhite") {setBGColor()}
</pre>
<p>You can assign the function to an event handler in either of the following ways:
</p>
<pre>1. document.form1.colorButton.onclick=setBGColor
2. &lt;INPUT NAME="colorButton" TYPE="button"
      VALUE="Change background color"
      onClick="setBGColor()"&gt;
</pre>
<p>Creating the variable <code>setBGColor</code> shown above is similar to declaring the following function:
</p>
<pre>function setBGColor() {
   document.bgColor='antiquewhite'
}
</pre>
<p>Assigning a function to a variable is similar to declaring a function, but there are differences:
</p>
<ul><li> When you assign a function to a variable using var <code>setBGColor = new Function("...")</code>, <code>setBGColor</code> is a variable for which the current value is a reference to the function created with new <code>Function()</code>.
</li><li> When you create a function using <code>function setBGColor() {...}</code>, <code>setBGColor</code> is not a variable, it is the name of a function.
</li></ul>
<p>You can nest a function within a function. The nested (inner) function is private to its containing (outer) function:
</p>
<ul><li> The inner function can be accessed only from statements in the outer function.
</li><li> The inner function can use the arguments and variables of the outer function. The outer function cannot use the arguments and variables of the inner function.
</li></ul>
<p>{{template.PreviousNext("Core_JavaScript_1.5_Guide:Predefined_Core_Objects:Date_Object", "Core_JavaScript_1.5_Guide:Predefined_Core_Objects:Math_Object")}}
</p>{{ wiki.languages( { "ja": "ja/Core_JavaScript_1.5_Guide/Predefined_Core_Objects/Function_Object", "pl": "pl/Przewodnik_po_j\u0119zyku_JavaScript_1.5/Obiekty_predefiniowane/Obiekt_function" } ) }}
Revert to this revision