Function

  • Enlace amigable (slug) de la revisión: Referencia_de_JavaScript_1.5/Objetos_globales/Function
  • Título de la revisión: Function
  • Id de la revisión: 127858
  • Creada:
  • Creador: Mgjbot
  • ¿Es la revisión actual? No
  • Comentario robot Añadido: [[en:Core JavaScript 1.5 Reference:Global Objects:Function]] <<langbot>>

Contenido de la revisión

Summary

Core Object

Every function in JavaScript is actually a Function object.

Created by

As all other objects, Function objects can be created using the new statement:

new Function ([arg1[, arg2[, ... argN]],] functionBody)
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.

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

Description

General

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.

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");

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

The preceding code assigns a function to the variable multiply. To call the Function object, you can specify the variable name as if it were a function, as shown in the following examples.

var theAnswer = multiply(7, 6);

var myAge = 50;
if (myAge >= 39)
  myAge = multiply(myAge, .5);

Properties

arguments {{template.Deprecated_inline()}} : An array corresponding to the arguments passed to a function. This is deprecated as property of Function, use the arguments object available within the function instead.

arity {{template.Deprecated_inline()}} : Specifies the number of arguments expected by the function. Use the length property instead.

caller: Specifies the function that invoked the currently executing function (non-standard).

constructor: Specifies the function that creates an object's prototype.

length: Specifies the number of arguments expected by the function.

name: The name of the function (not part of the standard).

prototype: Allows the addition of properties to function objects (both those constructed using Function and those that were declared using a function declaration or a function expression).

Methods

apply: Allows you to apply the method of another object in the context of a different object (the calling object).

call: Allows you to call (execute) a method of another object in the context of a different object (the calling object).

toSource: Returns a string representing the source code of the function. Overrides the Object.toSource method.

toString: Returns a string representing the source code of the function. Overrides the Object.toString method.

valueOf: Returns a string representing the source code of the function. Overrides the Object.valueOf method.

Examples

Example: Creating "focus" and "blur" event handlers for a frame

The following example creates onFocus and onBlur event handlers for a frame. This code exists in the same file that contains the frameset tag. Note that scripting is the only way to create "focus" and "blur" event handlers for a frame, because you cannot specify the event handlers in the frame tag.

var frame = frames[0];
frame.onfocus = new Function("document.body.style.backgroundColor = 'white';");
frame.onblur = new Function("document.body.style.backgroundColor = '#bbbbbb';");

See also

Functions

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

Fuente de la revisión

<p>
</p>
<h3 name="Summary"> Summary </h3>
<p><b>Core Object</b>
</p><p>Every function in JavaScript is actually a <code>Function</code> object.
</p>
<h3 name="Created_by"> Created by </h3>
<p>As all other objects, <code>Function</code> objects can be created using the <code>new</code> statement:
</p>
<pre class="eval">new Function ([<i>arg1</i>[, <i>arg2</i>[, ... <i>argN</i>]],] <i>functionBody</i>)
</pre>
<dl><dt> <code>arg1, arg2, ... arg<i>N</i></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>
<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="Description"> Description </h3>
<h4 name="General"> General </h4>
<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>
<h4 name="Specifying_arguments_with_the_Function_constructor"> 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");
</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><p>The preceding code assigns a function to the variable <code>multiply</code>. To call the <code>Function</code> object, you can specify the variable name as if it were a function, as shown in the following examples.
</p>
<pre class="eval">var theAnswer = multiply(7, 6);

var myAge = 50;
if (myAge &gt;= 39)
  myAge = multiply(myAge, .5);
</pre>
<h3 name="Properties"> Properties </h3>
<p><a href="es/Core_JavaScript_1.5_Reference/Global_Objects/Function/arguments">arguments</a> {{template.Deprecated_inline()}} : An array corresponding to the arguments passed to a function. This is deprecated as property of <code>Function</code>, use the <a href="es/Core_JavaScript_1.5_Reference/Functions/arguments">arguments</a> object available within the function instead.
</p><p><a href="es/Core_JavaScript_1.5_Reference/Global_Objects/Function/arity">arity</a> {{template.Deprecated_inline()}} : Specifies the number of arguments expected by the function. Use the <a href="es/Core_JavaScript_1.5_Reference/Objects/Function/length">length</a> property instead.
</p><p><a href="es/Core_JavaScript_1.5_Reference/Global_Objects/Function/caller">caller</a>: Specifies the function that invoked the currently executing function (non-standard).
</p><p><a href="es/Core_JavaScript_1.5_Reference/Global_Objects/Function/constructor">constructor</a>: Specifies the function that creates an object's prototype.
</p><p><a href="es/Core_JavaScript_1.5_Reference/Global_Objects/Function/length">length</a>: Specifies the number of arguments expected by the function.
</p><p><a href="es/Core_JavaScript_1.5_Reference/Global_Objects/Function/name">name</a>: The name of the function (not part of the standard).
</p><p><a href="es/Core_JavaScript_1.5_Reference/Global_Objects/Function/prototype">prototype</a>: Allows the addition of properties to function objects (both those constructed using <code>Function</code> and those that were declared using a function declaration or a function expression).
</p>
<h3 name="Methods"> Methods </h3>
<p><a href="es/Core_JavaScript_1.5_Reference/Global_Objects/Function/apply">apply</a>: Allows you to apply the method of another object in the context of a different object (the calling object).
</p><p><a href="es/Core_JavaScript_1.5_Reference/Global_Objects/Function/call">call</a>: Allows you to call (execute) a method of another object in the context of a different object (the calling object).
</p><p><a href="es/Core_JavaScript_1.5_Reference/Global_Objects/Function/toSource">toSource</a>: Returns a string representing the source code of the function.  Overrides the <a href="es/Core_JavaScript_1.5_Reference/Global_Objects/Object/toSource">Object.toSource</a> method.
</p><p><a href="es/Core_JavaScript_1.5_Reference/Global_Objects/Function/toString">toString</a>: Returns a string representing the source code of the function.  Overrides the <a href="es/Core_JavaScript_1.5_Reference/Global_Objects/Object/toString">Object.toString</a> method.
</p><p><a href="es/Core_JavaScript_1.5_Reference/Global_Objects/Function/valueOf">valueOf</a>: Returns a string representing the source code of the function.  Overrides the <a href="es/Core_JavaScript_1.5_Reference/Global_Objects/Object/valueOf">Object.valueOf</a> method.
</p>
<h3 name="Examples"> Examples </h3>
<h4 name="Example:_Creating_.22focus.22_and_.22blur.22_event_handlers_for_a_frame"> Example: Creating "focus" and "blur" event handlers for a frame </h4>
<p>The following example creates <code>onFocus</code> and <code>onBlur</code> event handlers for a frame. This code exists in the same file that contains the <code>frameset</code> tag. Note that scripting is the only way to create "focus" and "blur" event handlers for a frame, because you cannot specify the event handlers in the <code>frame</code> tag.
</p>
<pre>var frame = frames[0];
frame.onfocus = new Function("document.body.style.backgroundColor = 'white';");
frame.onblur = new Function("document.body.style.backgroundColor = '#bbbbbb';");
</pre>
<h3 name="See_also"> See also </h3>
<p><a href="es/Core_JavaScript_1.5_Reference/Functions">Functions</a>
</p>
<div class="noinclude">
</div>
{{ wiki.languages( { "en": "en/Core_JavaScript_1.5_Reference/Global_Objects/Function", "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/Function", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Function" } ) }}
Revertir a esta revisión