-
Notifications
You must be signed in to change notification settings - Fork 8k
/
index.md
63 lines (44 loc) · 2.25 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
---
title: expresión function*
slug: Web/JavaScript/Reference/Operators/function*
---
{{jsSidebar("Operators")}}
La palabra clave **`function*`** puede ser utilizada para definir una función generadora en una expresión.
{{EmbedInteractiveExample("pages/js/expressions-functionasteriskexpression.html")}}
El código fuente de este ejemplo interactivo se encuentra almacenado en un repositorio GitHub. Si le gustaría contribuir al proyecto de ejemplos interactivos, por favor clone <https://github.com/mdn/interactive-examples> y envíenos un pull request.
## Sintaxis
```
function* [nombre]([param1[, param2[, ..., paramN]]]) {
sentencias
}
```
### Parámetros
- `nombre`
- : Nombre de la función. Puede ser omitido, en cuyo caso la función es _anínima_. El nombre sólo es local al cuerpo de la función.
- `paramN`
- : Nombre de un parámetro a ser pasado a la función. Una función puede tener hasta 255 argumentos.
- `sentencias`
- : Sentencias que componen el cuerpo de la función.
## Descripción
Una expresión `function*` es muy similar y tiene casi la misma sintaxis que una {{jsxref('Statements/function*', 'function* statement')}}. La principal diferencia entre una expresión `function*` y una sentencia `function*` es el _nombre de la función_, que en expresiones `function*` puede ser omitido para creaar funciones generadoras _anónimas_. Para más información vea también el capítulo acerca de [funciones](/es/docs/Web/JavaScript/Reference/Functions).
## Ejemplos
El siguiente ejemplo define una función generadora sin nombre y la asigna a `x`. La función produce el cuadrado de su argumento:
```js
var x = function* (y) {
yield y * y;
};
```
## Especificaciones
{{Specifications}}
## Compatibilidad con navegadores
{{Compat}}
## Vea también
- {{jsxref("Statements/function*", "Sentencia function*")}}
- Objeto {{jsxref("GeneratorFunction")}}
- [El protocolo iterable](/es/docs/Web/JavaScript/Referencia/Iteration_protocols)
- {{jsxref("Operators/yield", "yield")}}
- {{jsxref("Operators/yield*", "yield*")}}
- Objeto {{jsxref("Function")}}
- {{jsxref("Statements/function", "Sentencia function")}}
- {{jsxref("Operators/function", "Expresión function")}}
- {{jsxref("Functions_and_function_scope", "Funciones y alcance de funciones")}}