mozilla

Revision 1361 of Function

  • Revision slug: JavaScript/Reference/Global_Objects/Function
  • Revision title: Function
  • Revision id: 1361
  • Created:
  • Creator: wizard04
  • Is current revision? No
  • Comment Reverted to earlier version; 2 words added, 5 words removed

Revision Content

 

Summary

Every function in JavaScript is actually a Function object.

Syntax

new Function ([arg1[, arg2{{ mediawiki.external(', ... argN') }}],] functionBody)

Parameters

arg1, arg2, ... argN
Names to be used by the function as formal argument names. Each must be a string that corresponds to a valid JavaScript identifier or a list of such strings separated with a comma; for example "x", "theValue", or "a,b".
functionBody
A string containing the JavaScript statements comprising the function definition.

Description

Function objects created with the Function constructor 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 parsed only once.

All arguments passed to the function are treated as the names of the identifiers of the parameters in the function to be created, in the order in which they are passed.

Invoking the Function constructor as a function (without using the new operator) has the same effect as invoking it as a constructor.

Properties

{{ Js_see_prototype("Function", "Properties") }}

prototype
Allows the extension of all Function objects.

{{ JSInherits("Function", "Properties", "caller", "constructor", "length", "name") }}

Methods

{{ Js_see_prototype("Function", "Methods") }}

Although the Function object does not provide any methods of its own, it does inherit methods through the prototype chain.

{{ JSInherits("Function", "Methods", "apply", "call", "toSource", "toString", "valueOf") }}

{{ JSInherits("Object", "Methods", "__defineGetter__", "__defineSetter__", "hasOwnProperty", "isPrototypeOf", "__lookupGetter__", "__lookupSetter__", "__noSuchMethod__", "propertyIsEnumerable", "unwatch", "watch") }}

Function instances

{{ :en/Core_JavaScript_1.5_Reference/Global_Objects/Function/prototype() }}

Examples

Example: Specifying arguments with the Function constructor

The following code creates a Function object that takes two arguments.

var multiply = new Function("x", "y", "return x * y");
var theAnswer = multiply(7, 6);

The arguments "x" and "y" are formal argument names that are used in the function body, "return x * y".

See also

{{ languages( { "es": "es/Referencia_de_JavaScript_1.5/Objetos_globales/Function", "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/Function", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Function", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Function" } ) }}

Revision Source

<p>
 </p>
<h3 name="Summary"> Summary </h3>
<p>Every function in JavaScript is actually a <code>Function</code> object.
</p>
<h3 name="Syntax"> Syntax </h3>
<p><code>new Function ([<em>arg1</em>[, <em>arg2</em>{{ mediawiki.external(', ... <i>argN</i>') }}],] <em>functionBody</em>)</code>
</p>
<h3 name="Parameters"> Parameters </h3>
<dl><dt> <code>arg1, arg2, ... arg<em>N</em></code>
</dt><dd> Names to be used by the function as formal argument names. Each must be a string that corresponds to a valid JavaScript identifier or a list of such strings separated with a comma; for example "<code>x</code>", "<code>theValue</code>", or "<code>a,b</code>".
</dd></dl>
<dl><dt> <code>functionBody</code>
</dt><dd> A string containing the JavaScript statements comprising the function definition.
</dd></dl>
<h3 name="Description"> Description </h3>
<p><code>Function</code> objects created with the <code>Function</code> constructor 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 parsed only once.
</p><p>All arguments passed to the function are treated as the names of the identifiers of the parameters in the function to be created, in the order in which they are passed.
</p><p>Invoking the <code>Function</code> constructor as a function (without using the <code>new</code> operator) has the same effect as invoking it as a constructor.
</p>
<h3 name="Properties"> Properties </h3>
<p>{{ Js_see_prototype("Function", "Properties") }}
</p>
<dl><dt> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Function/prototype">prototype</a>
</dt><dd> Allows the extension of all <code>Function</code> objects.
</dd></dl>
<p>{{ JSInherits("Function", "Properties", "caller", "constructor", "length", "name") }}
</p>
<h3 name="Methods"> Methods </h3>
<p>{{ Js_see_prototype("Function", "Methods") }}
</p><p>Although the <code>Function</code> object does not provide any methods of its own, it does inherit methods through the prototype chain.
</p><p>{{ JSInherits("Function", "Methods", "apply", "call", "toSource", "toString", "valueOf") }}
</p><p>{{ JSInherits("Object", "Methods", "__defineGetter__", "__defineSetter__", "hasOwnProperty", "isPrototypeOf", "__lookupGetter__", "__lookupSetter__", "__noSuchMethod__", "propertyIsEnumerable", "unwatch", "watch") }}
</p>
<h3 name="Function_instances"> <code>Function</code> instances </h3>
<p>{{ :en/Core_JavaScript_1.5_Reference/Global_Objects/Function/prototype() }}
</p>
<h3 name="Examples"> Examples </h3>
<h4 name="Example:_Specifying_arguments_with_the_Function_constructor"> Example: Specifying arguments with the <code>Function</code> constructor </h4>
<p>The following code creates a <code>Function</code> object that takes two arguments.
</p>
<pre class="eval">var multiply = new Function("x", "y", "return x * y");
var theAnswer = multiply(7, 6);
</pre>
<p>The arguments "<code>x</code>" and "<code>y</code>" are formal argument names that are used in the function body, "<code>return x * y</code>".
</p>
<h3 name="See_also"> See also </h3>
<ul><li> <a href="en/Core_JavaScript_1.5_Reference/Operators/Special_Operators/function_Operator">function Operator</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Statements/function">function Statement</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Functions">Functions</a>
</li></ul>

{{ languages( { "es": "es/Referencia_de_JavaScript_1.5/Objetos_globales/Function", "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/Function", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Function", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Function" } ) }}
Revert to this revision