Referencia de JavaScript 1.5:Funciones:arguments
De MDC
Tabla de contenidos |
[editar] Resumen
Un objeto similar a un arreglo que se corresponde con los argumentos pasados a la función.
| Variable local dentro de todas las funciones y propiedad desaconsejada de Function | |
|---|---|
| Implementada en: | JavaScript 1.1, NES 2.0
JavaScript 1.2: añadida la propiedad JavaScript 1.3: propiedad desaconsejada JavaScript 1.4: desaconsejado |
| Versión ECMA: | ECMA-262 |
[editar] Descripción
The arguments object is a local variable available within all functions; arguments as a property of Function can no longer be used.
You can refer to a function's arguments within the function by using the arguments object. This object contains an entry for each argument passed to the function, the first entry's index starting at 0. For example, if a function is passed three arguments, you can refer to the argument as follows:
arguments[0] arguments[1] arguments[2]
The arguments can also be set:
arguments[1] = 'new value';
arguments[n] cannot be set if n is greater than the number of formal or actual parameters. This has been fixed in the engine for JavaScript 1.6.The arguments object is not an array. It is similar to an array, but does not have any array properties except length. For example, it does not have the pop method. However it can be converted to an real array:
var args = Array.prototype.slice.call(arguments);
The arguments object is available only within a function body. Attempting to access the arguments object outside a function declaration results in an error.
You can use the arguments object if you call a function with more arguments than it is formally declared to accept. This technique is useful for functions that can be passed a variable number of arguments. You can use arguments.length to determine the number of arguments passed to the function, and then process each argument by using the arguments object. (To determine the number of arguments declared when a function was defined, use the Function.length property.)
[editar] Propiedades
arguments.callee: Especifica el cuerpo de función de la función que se ejecuta en ese momento.
arguments.caller Plantilla:deprecated inline : Especifica el nombre de la función que invocó la función que se ejecuta en ese momento.
arguments.length: Especifica el número de argumentos pasados a la función.
[editar] Compatibilidad hacia atrás
[editar] JavaScript 1.3 y versiones anteriores
Además de estar disponible como variable local, el objeto arguments también es una propiedad del objeto Function y puede estar precedida por el nombre de la función. Por ejemplo, si se pasa a una función miFuncion tres argumentos llamados argumento1, argumento2, y argumento3, puede referirse a dichos argumentos como:
miFuncion.arguments[0] miFuncion.arguments[1] miFuncion.arguments[2]
[editar] JavaScript 1.1 y 1.2
Las siguientes características, que estaban disponibles en JavaScript 1.1 y JavaScript 1.2, han sido eliminadas:
- Cada variable local de una función es una propiedad del objeto
arguments. Por ejemplo, si una funciónmyFunctiene una variable local llamadamiVariableLocal, puede referirse a dicha variable comoarguments.miVariableLocal.
- Cada argumento formal de una función es una propiedad del objeto
arguments. Por ejemplo, si una funciónmiFunciontiene dos argumentos llamadosargumento1yargumento2, puede referirse a dichos argumentos comoarguments.argumento1yarguments.argumento2. (También puede referirse a ellos comoarguments[0]yarguments[1].)
[editar] Ejemplos
[editar] Ejemplo: Defining function that concatenates several strings
This example defines a function that concatenates several strings. The only formal argument for the function is a string that specifies the characters that separate the items to concatenate. The function is defined as follows:
function myConcat(separator) {
var result = "";
// iterate through non-separator arguments
for (var i = 1; i < arguments.length; i++)
result += arguments[i] + separator;
return result;
}
You can pass any number of arguments to this function, and it creates a list using each argument as an item in the list.
// returns "red, orange, blue, "
myConcat(", ", "red", "orange", "blue");
// returns "elephant; giraffe; lion; cheetah; "
myConcat("; ", "elephant", "giraffe", "lion", "cheetah");
// returns "sage. basil. oregano. pepper. parsley. "
myConcat(". ", "sage", "basil", "oregano", "pepper", "parsley");
[editar] Ejemplo: Defining a function that creates HTML lists
This example defines a function that creates a string containing HTML for a list. The only formal argument for the function is a string that is "u" if the list is to be unordered (bulleted), or "o" if the list is to be ordered (numbered). The function is defined as follows:
function list(type) {
var result = "<" + type + "l>";
// iterate through non-type arguments
for (var i = 1; i < arguments.length; i++)
result += "<li>" + arguments[i] + "</li>";
result += "</" + type + "l>"; // end list
return result;
}
You can pass any number of arguments to this function, and it adds each argument as an item to a list of the type indicated. For example:
var listHTML = list("u", "One", "Two", "Three");
// listHTML is "<ul><li>One</li><li>Two</li><li>Three</li></ul>"